Last answered:

11 Sept 2023

Posted on:

11 Sept 2023

0

Resolved: can anyone help me with this

num1 = int(input('enter a number'))
num2 = int(input('enter a number'))
while (num1 or num2 > 100) or (num1 or num2 < 0) or num1 == num2:
    print(' must be diff num between 1-100, try again')
    num1 = int(input('enter a number'))
    num2 = int(input('enter a number'))
if num1 > num2:
    for i in range(num2,num1+1):
        print(i,end=' ')
else:
    for i in range(num1,num2+1):

        print(i,end=' ') 

     

    

  when i tried it this way, the code never runs the IF block of code, no matter what numeber i enter

2 answers ( 2 marked as helpful)
Instructor
Posted on:

11 Sept 2023

0

Hey Amazigh,


Thank you for reaching out!


Let me address the conditions following the while-statement, namely:

(num1 or num2 > 100) or (num1 or num2 < 0) or num1 == num2


The way you've written the code, you have the following conditions:

- num1

- num2 > 100

- num1

- num2 < 0

- num1 == num2


Each of these conditions can be either True or False. Note that num1 (and num2) can also be considered conditions and will be evaluated as True for every integer that you enter. You can convince yourself of that by executing, for example, the following line of code:

bool(2)

which will return True.


That being said, consider we enter the following numbers:

- num1 = 71

- num2 = 72

The conditions will be evaluated as follows:

- num1 -> True

- num2 > 100 -> False

- num1 -> True

- num2 < 0 -> False

- num1 == num2 -> False

You therefore have at least one condition evaluated as True. And since you are using the OR logical operator, you will always obtain True regardless of the outcome of the remaining conditions (remember that

- TRUE OR FALSE = TRUE

- TRUE OR TRUE = TRUE).

You will thus never reach the if-statement.

 

The way you'd instead want your conditions to be is the following:

- num1 > 100 

- num2 > 100

- num1 < 0

- num2 < 0

- num1 == num2


In this way, you check whether both num1 or num2 are larger than 100 or smaller than 0.


Hope this helps.


Kind regards,

365 Hristina

Posted on:

11 Sept 2023

0

num1 = int(input('enter a number'))
num2 = int(input('enter a number'))
while (num1 or num2) > 100 or (num1 or num2) < 0 or num1 == num2:
    print(' must be diff num between 1-100, try again')
    num1 = int(input('enter a number'))
    num2 = int(input('enter a number'))
if num1 > num2:
    for i in range(num2,num1+1):
        print(i,end=' ')
else:
    for i in range(num1,num2+1):

        print(i,end=' ')

i changed the brackets to this :

while (num1 or num2) > 100 or (num1 or num2) < 0 or num1 == num2:                                                         and now it works. 

                                                                                                              Thank you 

Submit an answer