货币转换器问题

时间:2014-12-06 20:55:30

标签: python

我试图制作货币转换器,但是"而#34;当我选择选项2或3时,循环不起作用。选择选项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")

3 个答案:

答案 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
     .....

答案 1 :(得分:0)

您的while循环的条件为userChoice == "1";换句话说,当用户选择第一个选项时,继续运行程序,退出第二个或第三个选项。相反,您可能希望它继续运行您的程序,直到选择退出。为此,您需要将条件替换为userChoice != "3"

答案 2 :(得分:0)

你的代码有一些缺陷,主要是因为用户选择链接到while循环,而是应该链接到if / elif / else条件树:

另外,你的代码重复了很多次,这是一种不好的做法

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以及它们之间的比率。休息一下 在userChoice'3'上设置离开循环。
  5. 变量'value',取用户输入,格式化正在使用 字符串格式化(%符号),它采用currency_to和 currency_from。
  6. converted_value发送'value'和'ratio',然后捕获 新价值
  7. 再次使用字符串格式显示转换后的值。
  8. 再次打印主菜单
  9. try / except位以防用户输入无效选项 菜单,它会引发NameError,因为currency_from, currency_to和ratio不存在。
相关问题