I am a beginner in python and currently I am studying loops. I am not able to answer a question

Question:

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. How can I write the correct code?

Asked By: vrhulk

||

Answers:

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")
Answered By: Samwise

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
Answered By: Yash Mehta
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.