Python Forum
Calculator won't subtract?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator won't subtract?
#1
I've just started GCSE computer science about a month ago, I already knew the basics of python like variables, if and else statements and stuff, and this is the first big task we've done. We're making a calculator and for some reason, when you ask it to subtract, it adds the numbers together. I have no clue why, I even showed it to my teacher who had no clue why it was adding instead of subtracting. I took it home to try and work on and I'm still not understanding the problem. I'm not sure what version of python we use in school but I'm on 3.8.0 on my laptop. Both my laptop and my school's run windows 7. Can anyone help me?

import time

print ("Hi! I'm Carrie the Calculator. :)")
time.sleep(1)
print ("What would you like to do?")
time.sleep(1)

command = input("Add or Subtract?")
if command == "ADD" or "add" or "plus" or "addition" or "Addition" or "Add" or "A" or "a":
    print ("Oh, addition! This will be fun")
    time.sleep(1)
    input1 = input("What is the 1st number?")
    input2 = input("What is the 2nd number?")
    number1 = int(input1)
    number2 = int(input2)
    result = number1 + number2
    output = str(result)
    time.sleep(0.3)
    print(input1 + " + " + input2 + " = " + output)

elif command == "Subtract" or "SUBTRACT" or "Take away" or "take away" or "subtract" or "sub" or "s" or "S":
    print ("Subtraction! I'm great at that!")
    time.sleep(1)
    input1 = input("What is the 1st number?")
    input2 = input("What is the 2nd number?")
    number1 = int(input1)
    number2 = int(input2)
    result = number1 - number2
    output = str(result)
    time.sleep(0.3)
    print(input1 + " - " + input2 + " = " + result)

else: print ("Sorry, that isn't something I can do. :(")
Reply
#2
This is because “if” condition is always True.
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
(Oct-17-2019, 04:17 PM)perfringo Wrote: This is because “if” condition is always True.

How do you mean? If I type Subtract that should ignore the first section shouldn't it? Since Subtract isn't listed in the first "if" line.
Reply
#4
Truth value testing on non-empty strings is True and you have lot of them after “or”.

You should use “if command.lower() in [list of lowercase variants]:”
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
#5
I was able to run it by including full evaluations between each "or".

Example:
if command == "a" or command == "add" or command == "Addition":
Reply
#6
It's better to use the in operator and the lower method. This:

if command == "ADD" or "add" or "plus" or "addition" or "Addition" or "Add" or "A" or "a":
becomes:

if command.lower() in ('add', 'plus', 'addition', 'a'):
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(Oct-17-2019, 04:43 PM)perfringo Wrote: Truth value testing on non-empty strings is True and you have lot of them after “or”.

You should use “if command.lower() in [list of lowercase variants]:”

(Oct-17-2019, 05:37 PM)ichabod801 Wrote: It's better to use the in operator and the lower method. This:

if command == "ADD" or "add" or "plus" or "addition" or "Addition" or "Add" or "A" or "a":
becomes:

if command.lower() in ('add', 'plus', 'addition', 'a'):

Thanks! This worked! Carrie can subtract now :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  given 2 base 20 numbers write a program to subtract second from the first and return dp_tisha 2 3,747 Jul-06-2017, 09:22 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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