Andrew Bodin
Andrew Bodin

Reputation: 55

Storing Multiple User inputs while using a loop

My intent here this to query the user for multiple inputs using a loop that stops when the user inputs and integer of zero. I need to be able to recall the data in a later line of code. With that in mind I'm trying to create a list of the users input.

Python 3 code

i = False
val1 = []
while i == False:
    if  val1 != 0:
        val1 = eval(input("Enter an integer, the value ends if it is 0: "))
    else:
        i = True
        print(val1)

Upvotes: 0

Views: 4593

Answers (1)

jh314
jh314

Reputation: 27802

I think it would be cleaner if you use a infinite loop and break if the input is 0. Otherwise, simply append to the the list.

values = []
while True:
    val = int(input("Enter an integer, the value ends if it is 0: "))
    if val == 0:
        break
    values.append(val)

Upvotes: 1

Related Questions