Brian
Brian

Reputation: 151

Get input for a while statement

while int(input("Input an integer (0 terminates): ")) != 0:
    #do stuff to the input

How would I store the input for the line the user entered in the line above.

Upvotes: 0

Views: 55

Answers (3)

gravetii
gravetii

Reputation: 9654

while 1:
    n = input("Input an integer (0 terminates): ")
    if n == 0:
        break
    # do something with n

Upvotes: 0

Maxime Lorant
Maxime Lorant

Reputation: 36181

The best and cleaner solution is to make an infinite loop and break it when the user input 0:

while True:
    inp = int(input("Input an integer (0 terminates): "))
    if inp == 0:
        break 
    print inp

Upvotes: 0

user2555451
user2555451

Reputation:

I think it would be best to make your while-loop something like this:

# loop continuously
while True:
    # get the input and store it in the variable inp
    inp = int(input("Input an integer (0 terminates): "))
    # break the loop if inp equals 0
    if inp == 0:
        break
    # do stuff to the input

Upvotes: 1

Related Questions