我试图制作货币转换器,但是"而#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")
答案 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: ")
以下是代码审核: