Q-> WAP in python to take input of different numbers from the user if the number if less than 100 keep on taking the input from the user and if the number entered is bigger than 100 display the message "congratulations! The number you entered is bigger than 100".We have to do this using for loop. The program that I wrote is:
a=list(input("enter a number"))
for a1 in a:
a=int(input())
if int(a1)<100:
print("try again")
continue
else:
print("congratulation the number you entered is more than 100")
break
but this program is faulty . please help me in writing the correct code.
CodePudding user response:
You don't need to build a list, since there's nothing you need the old numbers for. The most straightforward way to do this would be a while
loop, e.g.:
while int(input("enter a number ")) < 100:
print("try again")
print("congratulations! the number you entered is at least 100")
Using a for
loop doesn't make as much sense because you don't have a set number of things to iterate over; you need to just keep retrying while
the user is entering number that's too small. If I had to use for
here, though, I'd use something like itertools.count
:
from itertools import count
for _ in count():
if int(input("enter a number ")) >= 100:
print("congratulations!")
break
print("try again")
CodePudding user response:
Code:-
for _ in iter(int, 1):
a=input("Enter the number: ")
if int(a)>100:
print("Congratulation")
break
print("Your number is lower")
Output:-
Enter the number: 80
Your number is lower
Enter the number: 43
Your number is lower
Enter the number: 89
Your number is lower
Enter the number: 97
Your number is lower
Enter the number: 101
Congratulation