-12

我正在尝试将此伪代码转换为 Python。我不知道如何做到这一点。它看起来很简单,但我对 Python 一无所知,这让我几乎不可能做到。这是伪代码:

Main Module

Declare Option

Declare value

Declare cost

While(choice ==’Y’)

Write “Vehicle Shipping Rates to Africa”

Write “1. Car to Ghana”

Write “2. Van to Nigeria”

Write “3. Truck to Togo”

Write “4. Van to Kenya”

Write “5. Truck to Somalia”

Write “Enter the choice:”

Get option

Write “Enter the car price:”

Get value

if ( option = 1)

cost = value / 0.30;

write “It would cost $”+cost "to ship a car that cost $” +value+"  to Ghana."

else if (option = 2)

cost = value / 0.20;

write “It would cost $”+cost "to ship a car that cost $” +value+"  to Nigeria."

else if ( option = 3)

cost = value / 0.33;

write “It would cost $”+cost "to ship a car that cost $” +value+"  to Togo."

else if (option = 4)

cost = value / 0.17;

write “It would cost $”+cost "to ship a car that cost $” +value+"  to Kenya."

else if ( option = 5)

cost = value / 0.31;

write “It would cost $”+cost "to ship a car that cost $” +value+"  to Somalia."

else

write “This is not a valid selection” “Please try again.”

endif

Write “Vehicle price you entered:”, value

Write “Shipping cost:”, cost

Write “Would you like to choose another selection, Y=Yes or N=No.”

Get choice

End while

Write “Thank you our application.”

End main module
4

1 回答 1

0

在 Stack Overflow 上摆姿势之前,您应该尝试编写一些代码。这将起作用,但不会捕获任何错误情况。

options = [
    {'vehicle': 'Car', 'destination': 'Ghana', 'coeff': 0.3},
    {'vehicle': 'Van', 'destination': 'Nigeria', 'coeff': 0.2},
    {'vehicle': 'Truck', 'destination': 'Togo', 'coeff': 0.33},
    {'vehicle': 'Van', 'destination': 'Kenya', 'coeff': 0.17},
    {'vehicle': 'Truck', 'destination': 'Somalia', 'coeff': 0.31},
]
while True:
    print("Vehicle Shipping Rates to Africa")
    for i, opt in enumerate(options):
        print("%i. %s to %s" % (i+1, opt['vehicle'], opt['destination']))
    option = options[int(raw_input("Enter the choice:"))]
    value = float(raw_input("Enter the car price:"))
    cost = value / option['coeff']
    print("It would cost $%s to ship a %s that cost $%s to %s." % (cost, option['vehicle'], value, option['destination']))
    print("Vehicle price you entered: %s" % value)
    print("Shipping cost: %s" % cost)
    again = raw_input("Would you like to choose another selection, Y=Yes or N=No.")
    if again.lower() == 'n':
        break
print("Thank you our application.")
于 2013-10-06T21:04:34.390 回答