Reputation: 11
A while back I was tasked to design a currency converter as part of my school project. I remember including a 'while loop' which worked perfectly. However, when I looked back over the program again and tested it, the while loop no longer worked.
I'm hoping someone could explain why the while loop isn't working. Maybe it never worked and I'm just remembering incorrectly, or maybe something changed within the code which has caused it to not work?
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
Thanks, please explain simply because I'm not very experienced with the program!
EDIT: Formatting error. Real code looks like this: Currency Converter
Upvotes: 0
Views: 173
Reputation: 1139
You have 2 nested loops here, so the second one needs two be indented :
while True:
while True: #requires indentation
currentcurrency = input ("Select a starting currency: ")
[...]
Upvotes: 2