0

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)
2
  • 1
    Under if val1 != 0: you should not assign, but .append to a list. Like this: val1.append(eval...) Commented Sep 30, 2015 at 18:47
  • it will allow me to append the list however, I still run into the problem of it not breaking out of the loop by running the else. Is there a solution to check to see if the list contains a 0 in to break out? Essentially I want to call the min and max out of the list at the very end. Commented Sep 30, 2015 at 19:24

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

I think you need to wrap the input call in a call to int, otherwise val == 0 will never be true.
input(prompt) is equivalent to eval(raw_input(prompt)), so it should work
This solution partially works, however it doesn't seem to break out after entering a 0 value. If you run it it asks me for another input from the user. We need the int value of 0 to break out of the loop.
It works as intended for python2, but yeah, you need to convert to int for python3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.