0

Hi I'm trying to get multiple inputs in the form of a list, which is nested inside a list. I'm able to get the output but not able to break out from the loop.

values = []
while True:
    val = list(input("Enter an integer, the value ends if it is 'stop': "))
    if val == 0:
        exit()
    values.append(val)
    print(values)

I even used break..after the if condition..no Joy !!

Enter an integer, the value ends if it is 'stop': 4569
[['4', '5', '6', '9']]
Enter an integer, the value ends if it is 'stop': 666655
[['4', '5', '6', '9'], ['6', '6', '6', '6', '5', '5']]
Enter an integer, the value ends if it is 'stop': 5222
[['4', '5', '6', '9'], ['6', '6', '6', '6', '5', '5'], ['5', '2', '2', '2']]
Enter an integer, the value ends if it is 'stop': 0
[['4', '5', '6', '9'], ['6', '6', '6', '6', '5', '5'], ['5', '2', '2', '2'], ['0']]

This is the type of output i'm looking for but whenever I press 0...it does not break the loop..

also using if val == list(0) and if list(val) == list(0) and many other permutations either gives me an error or does not break out from the While Loop...Please Help..Thank you

3
  • In your example output you do not input 'stop' anywhere. Is this a mistake? It seems from the input message that 'stop' should end the current sub-list and start the next. Please clarify what inputting 'stop' and 0 should do. Commented Apr 4, 2021 at 17:35
  • Actually I got the solution...wasn't able to delete it...my mistake was if val == (['0']) Commented Apr 4, 2021 at 17:35
  • well i intended to use 'stop' or '0'...regardless it had to be a str value inside a list Commented Apr 4, 2021 at 17:37

1 Answer 1

1

The returned value of input is an instance of str. So in any case you would need to use the condition val == '0' in your if-statement. But since you are creating a list from the returned value of input you would need to change your code to the following:

values = []
while True:
    val = list(input("Enter an integer, the value ends if it is 'stop': "))
    if val == ['0']:
        exit()
    values.append(val)
    print(values)
Sign up to request clarification or add additional context in comments.

Comments

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.