Логический оператор OR | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Логический оператор OR

Я думал что для выполнения логического оператора OR достаточно чтобы было True первое утверждение. Но код который ниже проверяет две условия ( while цикл), т.е. проверяет значение первого и второго числа. Подскажите пожалуйста почему так? Ask the user for two numbers between 1 and 100. Then count from the lower number to the higher number. Print the results to the screen. ''' num_1 = int(input('Please enter a number between 1-100:> ')) num_2 = int(input('Please enter a number between 1-100:> ')) while num_1 < 0 or num_2 < 0 or num_1 > 100 or num_2 > 100 or num_1 == num_2: print('Numbers must be different values between 1 and 100, try again') num_1 = int(input('Please enter a number between 1-100:> ')) num_2 = int(input('Please enter a number between 1-100:> ')) if num_1 < num_2: for i in range(num_1,num_2+1): print(i,end=' ') else: for i in range(num_2,num_1+1): print(i,end=' ')

30th Mar 2020, 8:03 PM
Олег Петров
Олег Петров - avatar
1 Antwort
0
OR - if anyone condition evaluates to true the whole expression is True. having a while loop and 1 condition True is a jackpot, as unless you enter correct values you are trapped in an infinite loop. and if u wish to get to know which input is wrong, it would be better to multiple if conditions inside of the while loop like:- while True: if num_1 < 0 or num_1 > 100: num_1 = int(input('Please enter a number between 1-100:> ')) elif num_2 < 0 or num_2 > 100: num_2 = int(input('Please enter a number between 1-100:> ')) elif num_1 == num_2: num_1 = int(input('Please enter a number between 1-100:> ')) num_2 = int(input('Please enter a number between 1-100:> ')) By this, your code would be a bit longer than excepted but it should do your work just fine.
6th May 2020, 11:05 AM
Tejas Amit Sheth