简体   繁体   中英

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM