Python Forum
Calculator python3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Calculator python3 (/thread-24000.html)



Calculator python3 - Magicamtt455 - Jan-27-2020

I am trying to make a calculator in python for one of my classes.

Here is the code

n = 0
total = 0 
while n >= 0:
    n = int(input('Number? '))
    total = total + n
    if n == 'stop' or 'Stop':
       print(total)
    else:
      n = int(input('Number?'))
[Image: view?usp=sharing]

I keep on getting a syntax error what should I do?

I really need help.


RE: Calculator python3 - perfringo - Jan-27-2020

Problem is that if you receive 'stop' you continue with while loop. If n is 'stop' then comparison n >= 0 doesn't make sense.

In case of 'stop' you should break out of the loop.

However, this row of you code probably is not doing what you expect if n == 'stop' or 'Stop':.

'or' returns one of the operands. If first comparison is False (n is not equal to 'stop') then 'Stop' is returned and this is 'truthy' value for Python (means that condition is satisfied).


RE: Calculator python3 - ThiefOfTime - Jan-27-2020

First of all we have to take a look at the Error you are recieving. It is not a Syntax Error, a Syntax Error would mean, that you wrote code that can not be understood by the computer. What you get is a Value Error, which means, that the computer is trying to do operations where it expected a completely differend Value Type.
The problem you are having is that you directly convert the input to int. But since you want to also pass in words like "stop" this conversion will fail and result in a Value Error. I would suggest you change your code to something like this:
n = 0
total = 0 
while not (n == 'stop' or n == 'Stop'):
    n = input('Number? ')
    if not (n == 'stop' or n == 'Stop'):
       total = total + int(n)
       n = int(input('Number?'))
print(total)
Even when you save "Stop" inside of n, the comparison you used for the while-loop ( n >= 0 ) will result in an ValueError, when you send in the "stop" signal.

Though a bit more clean approach would be:
n = 0
total = 0 
while not n in ['stop', 'Stop']:
    n = input('Number? ')
    try:
       total = total + int(n)
    except ValueError:
       continue
print(total)
Here you accept that this error can occure but catch it through the try-except part. And since you only want to print the result when you are finished, you can do this after the while loop.

Also just keep one thing in mind. Using only a string in a condition will result to True if the string is not empty ( "" ) or False if it is empty. So the part where you said:
n == 'stop' or 'Stop'
will always resolve to True, since 'Stop' is always True.

@perfringo actually it is not only the comparison of the while-loop causing the error, but the conversion to int.

also sorry, for kind of double post


RE: Calculator python3 - perfringo - Jan-27-2020

(Jan-27-2020, 08:53 AM)ThiefOfTime Wrote: @perfringo actually it is not only the comparison of the while-loop causing the error, but the conversion to int.

Yes, you are correct and I made a mistake.

To catch more typing/error variations ('STOP', 'STop', etc) it is usually good to lower input.

input('Number: ').lower()
With Python 3.8 one can take advantage of walrus operator (no need to assign answer outside the loop):

while (answer := input('Number: ').lower()) != 'stop':
    # do stuff