货币转换器问题
问题描述:
我试图制作货币转换器,但是当我选择选项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")
答
替换此:
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
.....
答
你while
循环具有条件userChoice == "1"
;换句话说,当用户选择了第一个选择时,继续运行程序,退出第二个或第三个选择。相反,您可能希望它继续运行您的程序,直到已选择退出。要做到这一点,您需要用userChoice != "3"
替换条件。
+0
感谢您的帮助,我现在可以看到我犯的大量错误。 – JamesM 2014-12-06 21:28:37
答
你的代码有一些缺陷,主要是因为用户选择链接到该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: ")
这里有一个代码审查:
- 改变主菜单的顺序,以显示 选项的用户名拍摄后。
- 使main_menu成为一个变量,所以它可以在 转换后重复使用。
- 创建一个处理转换的函数,只留下 循环来设置。
- 循环获取userChoice,并根据它设置货币来源,currency_to和它们之间的比率。在userChoice'3'上设置的中断为 以离开循环。
- 变量'value',取用户输入,格式为 字符串格式化(%符号),需要currency_to和 currency_from。
- converted_value发送'value'和'ratio',并捕获 新值
- 转换后的值将再次使用字符串格式化显示。
- 主菜单再次
- 的试印刷/除位是在情况下,用户在 进入无效的选项菜单,它会引发NameError因为currency_from, currency_to和比率将不存在。
是什么让你通过'while'循环检查选项“1”,其他选项用'if'检查?将美元换算成英镑真的在概念上有很大不同,以至于它们需要不同的结构?请阅读关于互联网上任何教程中的'while'和'if'语句的部分,并且您将理解您的错误 - 此实现没有多大意义,并且此处还存在许多其他问题。 – BartoszKP 2014-12-06 20:59:23
很确定这个问题不是关于转换货币。 – 2014-12-06 21:02:38