Hello ,

I can't understand why this loop doesn't work as I wanted.
Why the continue statement isn't executed.
I want the code to count only the user input which is 1 or 2 and stop when the sum is >=25.

``````i = 0
user = input('Enter '1' or '2': ')

while True:
if ( user == '1' or user == '2' ):
i += int(user)
print('Sum is {0}'.format(i))
if i < 25:
continue
else:
break

else:
print('Invalid input')
user = input('Enter '1' or '2': ')
``````

Right know if I run the program it gives:

Enter '1' or '2': 1
Sum is 1
Sum is 2
Sum is 3
Sum is 4
.....
Sum is 25

3
Contributors
8
Replies
56
Views
3 Years
Discussion Span
Last Post by glao

Write pseudo code to show the logic of your code. I suggest

``````while True:
get user input
if input is invalid:
print error message
continue
process user input (update sum)
if exit condition is met:
break
``````

Hello ,

1) The 'get user input' , I think it's ok if I have it initially outside the loop,yes?

2) Doing something like:

``````i = 0

while True:
user = input('Enter '1' or '2': ')

if ( user == '1' or user == '2' ):
i += int(user)
print('Sum is {0}'.format(i))

else:
print('Invalid input')
user = input('Enter '1' or '2': ')
continue

if i >= 25:
break
``````

results in working generally ok , but when I enter:

Sum is 0
Enter '1' or '2':1
Sum is 1
Enter '1' or '2': 2
Sum is 3
Enter '1' or '2':2
Sum is 5
Enter '1' or '2': 1
Sum is 6
Enter '1' or '2': 4
Invalid input
Enter '1' or '2': 1
Enter '1' or '2':1
Sum is 7

It misses a count after the 'invalid input'.
I can't figure!

Thank you!

Hmm..

I changed to :

``````i = 0
while True:
user = input('Enter '1' or '2': ')
if ( user != '1' and user != '2' ):
print('Invalid input')
continue
else:
i += int(user)
print('Sum is {0}'.format(i))

if i >= 25:
break
``````

and it works!

But ,I can't figure why??

``````   if ( user == '1' or user == '2' ):
i += int(user)
``````

Now:

``````if ( user != '1' and user != '2' ):
print('Invalid input')
continue
``````

But , isn't it the same??

@glao you are not posting your actual code: `'Enter '1' or '2': '` is not a valid python string. Use copy and paste to write your code.
Your previous version failed because there was an extra `user = input(...)` in the invalid input part.
rproffitt's suggestion is not a bad thing, but it's not the issue here.
The rules of operator precedence for python are here.

Yes , it is

`````` input('Enter 1 or 2: ')
``````

Only this was wrong.But I can't understand why it misses a count after an invalid input when I use

``````if ( user == '1' or user == '2' ):

Your previous version failed because there was an extra user = input(...) in the invalid input part.

Ok for this,thanks
``````

Thank you!

But I can't understand why it misses a count after an invalid input when I use

It doesn't have anything to do with the if test. Look at your code and you will see that there are two lines `user = input(...)` instead of one.

Oh yes!You are right!
Thank you very much for your help!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.