Please help me understand why the second while does not work. For some reason, ask cannot change the value of two and therefore the loop is infinite.

one=0 while one!=1: speed=3*10**8# скорость света frequency=input("Введите номер канала wi-fi или введите 0, чтобы указать частоту вручную\n ")# частота канала if frequency=='1': print("Внешняя сторона квадрата антенны равна ", (speed/(2412*1000)/4)*1.01, "сантиметров") elif frequency=='2': print("Внешняя сторона квадрата антенны равна ", (speed/(2417*1000)/4)*1.01, "сантиметров") elif frequency=='3': print("Внешняя сторона квадрата антенны равна ", (speed/(2422*1000)/4)*1.01, "сантиметров") elif frequency=='4': print("Внешняя сторона квадрата антенны равна ", (speed/(2427*1000)/4)*1.01, "сантиметров") elif frequency=='5': print("Внешняя сторона квадрата антенны равна ", (speed/(2432*1000)/4)*1.01, "сантиметров") elif frequency=='6': print("Внешняя сторона квадрата антенны равна ", (speed/(2437*1000)/4)*1.01, "сантиметров") elif frequency=='7': print("Внешняя сторона квадрата антенны равна ", (speed/(2442*1000)/4)*1.01, "сантиметров") elif frequency=='8': print("Внешняя сторона квадрата антенны равна ", (speed/(2447*1000)/4)*1.01, "сантиметров") elif frequency=='9': print("Внешняя сторона квадрата антенны равна ", (speed/(2452*1000)/4)*1.01, "сантиметров") elif frequency=='10': print("Внешняя сторона квадрата антенны равна ", (speed/(2457*1000)/4)*1.01, "сантиметров") elif frequency=='11': print("Внешняя сторона квадрата антенны равна ", (speed/(2462*1000)/4)*1.01, "сантиметров") elif frequency=='12': print("Внешняя сторона квадрата антенны равна ", (speed/(2467*1000)/4)*1.01, "сантиметров") elif frequency=='13': print("Внешняя сторона квадрата антенны равна ", (speed/(2472*1000)/4)*1.01, "сантиметров") elif frequency=='0': two=0 while two!=1: frequency2=int(input("Введите частоту в мегагерцах\n "))# частота print("Внешняя сторона квадрата антенны равна ", (speed/(frequency2*1000)/4)*1.01, "сантиметров") ask=input("Запустить заново? Y/N\n ") if ask=="Y" or "y": two +=0 elif ask=="N" or "n": two +=1 else: print("Неверный номер канала. Укажите канал от 1 до 13\n ") question=input("Нажмите 1, чтобы произвести другой расчёт или 0, чтобы выйти\n ") if question=='1': one +=0 elif question=='0': one +=1 else: one +=1 

I took the advice and redid it. Thanks to all

  one=0 while one!=1: speed=3*10**8# скорость света frequency=int(input("Укажите номер канала wi-fi или введите 0, чтобы указать частоту вручную\n "))# частота канала if frequency in range(1,13):# диапазон каналов formula=(speed/((2407 + frequency*5)*1000*10))# формула длины волны для каналов wi-fi в сантиметрах print("Внешняя сторона квадрата антенны равна ", round(formula/4*1.01,2), "сантиметров") print("Размер сторон рефлектора ", round(formula*1.01,2),"сантиметров") print("Расстояние вибратор-рефлектор ", round(formula/8.7,2),"сантиметров") elif frequency==0: two=0 while two!=1: frequency2=float(input("Укажите частоту в мегагерцах\n "))# произвольная частота formula2=(speed/(frequency2*1000*10))# формула длины волны для произвольной частоты в сантиметрах print("Внешняя сторона квадрата антенны равна ", round(formula2/4*1.01,2), "сантиметров") print("Размер сторон рефлектора ", round(formula2*1.01,2), "сантиметров") print("Расстояние вибратор-рефлектор ", round(formula2/8.7*1.01,2), "сантиметров") ask=input("Указать другую частоту? Y/N\n ") if ask.upper()=="N":# .upper() - игнорирование регистра буквы two +=1 #break# прерывание цикла командой break else: print("Неверный номер канала. Укажите канал от 1 до 13\n ") question=input("Начать новый расчёт? Y/N\n ") if question in ('N', 'n'):# любой вариант ответа из перечисленных one +=1 
  • Use the formula (speed/((2407 + freq*5)*1000)/4)*1.01 instead of this monster ... - MaxU
  • and what is the meaning of two +=0 ? :) - gil9red
  • In general, I have nothing to do with programming, the last time at school many years ago was a couple of lessons on Pascal. two + = 0 so that the loop does not break, while there is a question and answer. - Nubi
  • one
    if ask=="Y" or "y" and elif ask=="N" or "n" do not what you think! - Enikeyschik
  • You have to continue the calculation will enter any character or even just Enter without typing. In general, this is not bad, just advise you to change the logic so that any_key leads to the most likely outcome. If output rather than calculating the next value is expected, the logic must be inverted. And yes, unify the check, either or or in . I also advise you to redo the exit to break . It will be easier to understand the code in a month :) - Lecron

1 answer 1

You have an error in the conditions:

 if ask=="Y" or "y": two +=0 elif ask=="N" or "n": two +=1 

The fact is that here you actually have two conditions - ask=="Y" and bool("y") . And since bool('y') will always be True , because a boolean check of a string is a check that it is not empty, and the or operator is used, then the check itself will be if ask=="Y" or True: which for any values ​​will return True .


It will be right:

 if ask == "Y" or ask == "y": two += 0 elif ask == "N" or ask == "n": two += 1 

Or a closer version to the logic of the question:

 # Альтернативный вариант с строкой: # if ask in "Yy": if ask in ("Y", "y"): two += 0 elif ask in ("N", "n"): two += 1 

Or, give the ask itself to the same register as the values ​​to be checked:

 ask = ask.upper() if ask == "Y": two += 0 elif ask == "N": two += 1 

Ps.

And so, it was enough if one 'a:

 if ask.upper() == "N": two += 1 
  • Thank. Took all the tips and rewrote - Nubi
  • @ Nubi, if my answer helped you, accept it. Click on the tick in the answer :) - gil9red