不久前,我的任务是设计一个货币兑换器,作为我学校项目的一部分。我记得包括了一个“while循环”,它非常有效。然而,当我再次回顾该程序并对其进行测试时,while循环不再起作用。在
我希望有人能解释while循环不起作用的原因。也许它从来没有起作用,我只是记错了,或者是代码中的某些变化导致它不能工作?在
while True: #first while loop - repeats entire program unless broken
while True: #second while loop - repeats input section of code unless broken
currentcurrency = input ("Select a starting currency: ") #allows user to input their starting currency, saved as a string in variable 'currentcurrency'
print ('You selected %s' % currentcurrency) #prints inputted string
value = float(input ("Input your current value: ")) #allows user to input their value of money, saved as a float in variable 'value'
newcurrency = input ("Select a new currency: ") #allows the user to input their new currency, saved as a string in variable 'newcurrency'
if currentcurrency == 'Pound' and newcurrency == 'Pound': #this part of the code determines the convertor value based upon the two inputted currencies
convertor = 1
if currentcurrency == 'Pound' and newcurrency == 'Euro':
convertor = 1.34
if currentcurrency == 'Pound' and newcurrency == 'Dollar':
convertor = 1.46
if currentcurrency == 'Pound' and newcurrency == 'Yen':
convertor = 171.61
if currentcurrency == 'Euro' and newcurrency == 'Pound':
convertor = 0.75
if currentcurrency == 'Euro' and newcurrency == 'Euro':
convertor = 1
if currentcurrency == 'Euro' and newcurrency == 'Dollar':
convertor = 1.09
if currentcurrency == 'Euro' and newcurrency == 'Yen':
convertor = 127.47
if currentcurrency == 'Dollar' and newcurrency == 'Pound':
convertor = 0.69
if currentcurrency == 'Dollar' and newcurrency == 'Euro':
convertor = 0.88
if currentcurrency == 'Dollar' and newcurrency == 'Dollar':
convertor = 1
if currentcurrency == 'Dollar' and newcurrency == 'Yen':
convertor = 121.12
if currentcurrency == 'Yen' and newcurrency == 'Pound':
convertor = 0.0062
if currentcurrency == 'Yen' and newcurrency == 'Euro':
convertor = 0.0076
if currentcurrency == 'Yen' and newcurrency == 'Dollar':
convertor = 0.0083
if currentcurrency == 'Yen' and newcurrency == 'Yen':
convertor = 1
print ("Do you want to convert", (value), (currentcurrency), "to", (newcurrency), "?")
answer = input ("Yes/No: ") #allows the user to input a response, saved in variable 'answer'
if answer == 'Yes' or 'y' or 'yes': #if the user inputted string is 'Yes' or equivelant,
break #the while loop will break, and the program will continue
result = (convertor) * (value) #calculates the result by multiplying the user inputted value and the converter
result *= 100 #this part of the code calculates the result to two decimal places
result += 0.5 #it multiplies the result by 100 and adds 0.5
result = int(result) #then changes the result to an integer
result /= float(100) #divides the result by a 100 and changes it to a float
print (result, newcurrency) #prints the result, followed by the new currency
answer2 = input("Do you want to convert again? (Yes/No): ") #allows the user to input an answer
if answer2 == 'No' or 'n' or 'no': #if the inputted answer is 'No' or equivalent,
break #the while loop breaks and the code continues - otherwise, it repeasts the program
exit() #ends the program
谢谢,请简单解释一下,因为我对这个项目不是很有经验!在
编辑:格式错误。实际代码如下所示: Currency Converter
这里有两个嵌套循环,因此第二个需要缩进两个:
相关问题 更多 >
编程相关推荐