2014-12-06 2 views
0

통화 변환기를 만들려고했으나 옵션 2 또는 3을 선택할 때 "while"루프가 작동하지 않습니다. 옵션 1을 선택하는 것은 좋지만 다른 두 개는 고장났습니다.통화 변환기 문제

##### Welcome Menu###### 
print("*************************************") 
print("* Queensmead currency converter *")     
print("* 1) Convert from Pound to Dollar *") 
print("* 2) Conver from Dollar to Pound *") 
print("* 3) Quit       *") 
print("*         *") 
print("*************************************") 

print ("Hello, Welcome to the Currency Converter. \nWhat is your name ? ") 
name = input() 

userChoice= input ("Hi " + name + ", Please choose one of the above options ") 

while userChoice == "1": 
    #Prompt the user the amount of Pound they want to convert 
    #Store what the user typed into a vairable 
    userGBP = int(input ("Enter the amount in Pound Stirling you wish to convert to US Dollars: ")) 

    dollar = userGBP * 1.55 
    #Returns the amount to user request 
    print ("£",userGBP , "=", dollar ,"US Dollars") 
    userChoice = input("If you like to convert some more money,please choose from the above Options ") 
    print (userChoice) 



    if userChoice =="2": 
    #Prompt the user the amount of Pound they want to convert 
    #Store what the user typed into a variable 
     userDollar = int(input ("Enter the amount in Dollars you wish to convert to Pound Stirling: ")) 

     pound = userDollar * 0.64 
     #Returns the amount to user request 
     print ("$",userDollar , "=", pound ,"Pound Stirling") 
     userChoice = input("If you like to convert some more money,please choose from the above Options ") 
     print (userChoice) 

    elif userChoice =="3": 
     userQuit = input ("") 
     print ("Thank you for using this program") 

    else: 
    print ("Error: You have entered invalid selection. Please try again") 
+0

'while' 루프를 사용하여 옵션 "1"을 확인하고'if'를 사용하여 다른 옵션을 확인하는 방법은 무엇입니까? 달러를 파운드로 변환하는 것이 개념적으로 매우 다르므로 다른 구조가 필요합니까? 인터넷상의 튜토리얼에서'while'과'if' 문에 대한 섹션을 읽어보십시오. 여러분은 실수를 이해할 것입니다.이 구현은별로 의미가 없으며 다른 많은 문제가 있습니다. – BartoszKP

+0

이 질문은 통화 변환에 관한 것이 아닙니다. –

답변

0

이 대체 :

while userChoice == "1": 

에 :

while True: 

같이 할 필요가있는 경우 :

while True: 
    #user input choice 
    if use_choice == 1: 
     #do_stuff 
    if use_choice == 2: 
     #do_stuff 
    if use_choice == 3: 
     break 
    ..... 
0

귀하의 while 루프 조건 userChoice == "1"있다; 즉, 사용자가 첫 번째 선택 사항을 선택하는 동안 프로그램을 계속 실행하고 두 번째 또는 세 번째 선택 항목에서 종료하십시오. 대신 종료 프로그램이으로 선택 될 때까지 프로그램을 계속 실행하기를 원할 것입니다. 그러려면 상태를 userChoice != "3"으로 바꿔야합니다.

+0

모든 도움을 주셔서 감사합니다. 이제 막 큰 실수를 볼 수 있습니다. – JamesM

0

당신의 코드가 사용자의 선택 대신, while 루프에 연결되어 그것이 경우/ELIF/다른 조건 트리에 연결되어야 주로하기 때문에, 약간의 결함을 가지고 또한

, 당신은 코드 자체를 많이 반복 , 그것은 코드 검토 나쁜 관행 여기

print("*************************************") 
print("* Queensmead currency converter *") 
print("*************************************") 

name = input("Hello, Welcome to the Currency Converter. \n" 
       "What is your name? ") 

main_menu = """ 
************************************* 
* 1) Convert from Pound to Dollar * 
* 2) Conver from Dollar to Pound * 
* 3) Quit       * 
*************************************""" 
print(main_menu) 
userChoice = input("Hi " + name + ", Please choose one of the above options: ") 


def convert_to(value, ratio): 
    return value * ratio 


while True: 
    if userChoice == "1": 
     currency_from = "Pound Stirling" 
     currency_to = "US Dolar" 
     ratio = 1.55 
    elif userChoice == "2": 
     currency_to = "Pound Stirling" 
     currency_from = "US Dolar" 
     ratio = 0.64 
    elif userChoice == "3": 
     print ("Thank you for using this program") 
     userQuit = input("") 
     break 
    else: 
     print ("Error: You have entered invalid selection. Please try again") 
    try: 
     value = int(input("Enter the amount in %s you wish to convert to %s: " % (currency_from, currency_to))) 
     converted_value = convert_to(value, ratio) 
     print ("%0.2f %s = %0.2f %s" % (value, currency_from, converted_value, currency_to)) 
    except NameError: 
     pass 
    input("Press ENTER to continue") 
    print main_menu 
    userChoice = input("If you like to convert some more money,please choose from the above Options: ") 

것입니다 :

  1. 이 주 메뉴의 순서를 변경하기 때문에 사용자 이름을 촬영 한 후 옵션 표시됩니다 .
  2. main_menu를 변수로 변경 했으므로 나중에 변환 후 재사용 할 수 있습니다.
  3. 루프를 그대로 두어 변환을 처리하는 함수를 만들었습니다.
  4. 루프는 userChoice를 가져오고 그에 따라 currency_from, currency_to 및 그 비율을 설정합니다. 휴식 시간은 이며 사용자가 '3'을 선택하면 루프를 종료합니다.
  5. 변수 'value'는 사용자 입력을 사용하고 형식 지정은 문자열 형식 지정 (% 기호)을 사용하며 currency_to 및 currency_from을 사용합니다.
  6. converted_value는 '값'과 '비율'을 전송하고 새 값
  7. 변환 된 값은 다시 문자열 형식을 사용하여 표시됩니다. 비트는 사용자가 의 메뉴를 유효하지 않은 옵션을 입력하는 경우에 제외 currency_from, currency_to과 비율이 존재하지 않기 때문에
  8. 메인 메뉴 다시
  9. 은 try 인쇄 /, 그것은 나가서 설명하자면 NameError를 올릴 것입니다.