2016-04-23 3 views
0

A while back Мне было поручено разработать конвертер валют в рамках моего школьного проекта. Я помню, что был включен цикл while. Однако, когда я снова просмотрел программу и протестировал ее, цикл while больше не работал.Пока Loop не работает

Я надеюсь, кто-то может объяснить, почему цикл 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 

Спасибо, объясните, пожалуйста, просто потому, что я не очень опытен с программой!

EDIT: ошибка форматирования. Реальный код выглядит следующим образом: Currency Converter

+2

Первый цикл не имеет тела, поэтому он никогда не сломается из петли. Вы имели в виду второй отступ, чтобы он был внутри первого? – Barmar

+3

вам понадобится еще один отступ со второй строки – Mateusz

+0

Почему вы не редактируете свой код, а не связываете изображение? –

ответ

2

У вас есть 2 вложенных циклов здесь, так что второй нужно два отступом:

while True: 
    while True: #requires indentation 
     currentcurrency = input ("Select a starting currency: ") 
     [...] 
+0

Не заметил этого, но я сделал ошибку форматирования при написании кода на этом сайте. У реального кода есть отступ во втором цикле - я вставил его изображение в исходное сообщение. Спасибо за ответ, тем не менее. –

+0

Какая ошибка возникает при запуске этого скрипта? –

Смежные вопросы