0

im trying to create a multiple values calculator in Python, using While, For/In sentences with lists

numbers = []
out = 0

while out == 0:
    numbers.append(int(input('Add a number: ')))
    out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
    if out2 == 1:
        out == 1

#In theory if out == 1 the while loop should end and go to:

for add in numbers:
    add = numbers
print(add)

I tried using While Not sentence, but i get an obvius mistake. I suppose this is a pretty dumb error of comprehention i have, but i can't really get what am i doing wrong. I'll be very glad if u help me with this.

josmanuel
  • 403
  • 1
  • 3
  • 10

2 Answers2

1

First of all, variable assignment should be done with = not ==. Your problem is that in the second input() (where you let user choose will he/she stay or exit), you need to convert the input to int from string first, OR check by the string representation of the number (fail-safe):

while out == 0:
    numbers.append(int(input('Add a number: ')))
    out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
    if out2 == '1':
        out = 1

However, best way to break out a loop is to use break:

while out == 0:
    numbers.append(int(input('Add a number: ')))
    out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
    if out2 == '1':
        break
Wasif
  • 14,755
  • 3
  • 14
  • 34
0

Apart from the errors pointed out in the other 2 answers, you are getting input from the user as a str and not converting it to an int.

So it never hits the if condition and therefore your program doesn't end.

Please try this

numbers = []
out = 0

while out == 0:
    numbers.append(int(input('Add a number: ')))
    out2 = input('''[0]To keep adding numbers
[1]To add and leave: ''')
    print(type(out2))
    if int(out2) == 1: # Convert the out2 to an int here
        print(f" Inside if condition")
        out = 1 # Use an assignment operator here
DineshKumar
  • 1,599
  • 2
  • 16
  • 30
  • 1
    Your answer is correct though All what you have written has been covered in other answers, perhaps other 2 answers are incomplete too, and that's why we don't need same answer repeated 2 times, also checking with `'1'` is better than `int(out2)` because if the input is non-int, then it will cast an error, you need to be fail safe though, still if you want to use it put it inside a `try..except` block, still you don't deserve downvotes, because of matter of time – Wasif Nov 08 '20 at 04:42
  • May be it was a matter of time.I got your answer only after I posted. Else I wouldn't have :) – DineshKumar Nov 08 '20 at 04:43
  • Lol, this is the solution, I never thought it would be that little thing. Thank you very much <3. From now on, I'll be careful about it. – josmanuel Nov 08 '20 at 04:54
  • Glad to be of help.Also, as @WasifHasan suggested, ensure the fail-check if the input is not an int. If it's a practice program it's fine. Else either have typecheck or check with "1" :) – DineshKumar Nov 08 '20 at 04:57