Home > Software engineering >  How to get correct number?
How to get correct number?

Time:10-10

--Python What's wrong in this code?

a=int(input('enter a number'))
b=list(range(1,11))
if a not in b:
   int(input('enter a number'))
else :
   print('ok')
    

output:

 enter a number 89
 enter a number 8

CodePudding user response:

Please explain a bit further what it is you're trying to do, if you're trying to output "OK" for the second input of '8', then you need to change the logic of this code.

a=int(input('enter a number'))

this line prints "enter a number" then your if statement checks if its within b but if it's not it prints "enter a number" again without doing anything with it, as seen in the next line:

int(input('enter a number'))

try using a for loop or while loop if you want your code to run through the if-else blocks more than once.

  • also, notice that you didn't put the input inside variable a again.

EDIT: try this code:

    print('enter a number')
a=int(input())
while a not in range(1,11):
    print('enter a number')
    a=int(input())
else :
   print('ok')
  • Related