Please give me the clarity on this

60 views Asked by At
num1=0
Stored_num=23

def input_num():
    num1=int(input('Enter the number again: '))

while num1!=stored_num:
    input_num()
else:
    print('Congrats ! The entered number mathched the stored number')

The code above is not accepting the else condition and not printing the message despite entering the stored value i.e. 23.

Please help me understand.

3

There are 3 answers

1
mushahidq On

You have named the variable Stored_num and are using stored_num to compare. Python is a case sensitive language so these are not the same. Change one of them to the other and it should work fine.

1
RAHUL MAURYA On

You have miss-typed the global variable and function scope variable.

num=0 Stored_num=23

def input_num(): num1=int(input('Enter the number again: '))

while num1!=stored_num:
    input_num()
else:
    print('Congrats ! The entered number mathched the stored number')

See, var Stored_num = 23 starts with capitals S, and inside stored_num start with small s. Make both the variable name the same and it will work.

3
Rikki On

Made few changes to your code:

num=0
Stored_num=23

def input_num():
    global num
    num=int(input('Enter the number again: '))

while num!=Stored_num:
    input_num()
else:
    print('Congrats ! The entered number mathched the stored number')

The outcome:

Enter the number again: 10
Enter the number again: 5
Enter the number again: 23
Congrats ! The entered number mathched the stored number

Process finished with exit code 0

Few notes:

  1. Python does not share the global scope variables. If you need to modify a variable defined outside of a def, you will need to announce it by using global
  2. Python is a case-sensitive language. (So Stored_num does not equal stored_num)

There was an error relating to num1 that didn't exist. Just renamed it to num.

I think that was it. (: