2013-10-06 3 views
-8

이 의사 코드를 파이썬으로 변환하려고합니다. 나는 이것을하는 방법에 대한 단서가 없습니다. 그것은 간단하게 보이지만 파이썬에 대한 지식이 없기 때문에 거의 불가능합니다. 이것은 의사 코드입니다.의사 코드를 파이썬으로 변환

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 
+5

SO는 "나를위한 코드 작성"이 아닙니다. 만약 당신이 그것을 줄 수 있다면, 우리는 당신이 잘못 될지도 모르는 곳에 조언을 해줄 수 있습니다. –

+0

http://learnpythonthehardway.org – MattDMo

+0

답장을 보내 주신 Daniel에게 감사드립니다. 나는 이것이 나를위한 코드를 작성하는 것이 아니라는 것을 알고있다. 나는 어디서부터 시작해야하는지 알고 싶다. 좋은 출발점은 좋을 것입니다. 다시 한번 감사드립니다. – user2852610

답변

0

스택 오버플로가 발생하기 전에 코드를 작성해야합니다. 이것은 작동하지만 오류 조건을 포착하지는 않습니다.

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.") 
+0

감사합니다 Graeme. 정말 고맙습니다. 저는 파이썬에 익숙하지 않습니다. 현재이 프로그래밍 언어를 배우려고합니다. – user2852610

+0

@ user2852610이 코드는 의사 코드의 1 : 1 번역이 아니지만 응축이 많이 발생하고 많은 파이썬 관용구를 사용합니다. – SethMMorton