Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator python3
#1
Photo 
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.
Reply
#2
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).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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
Reply
#4
(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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,820 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020