Calculator only does the + function - 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 only does the + function (/thread-19761.html) |
Calculator only does the + function - GalaxyCoyote - Jul-13-2019 So I am working on this software that has a calculator it, yesterday it was working fine but after adding some operations it seems to only do addition. here is a snip of the code. Calcnumone = 0 Calcnumtwo = 0 Calcopcmd = 0 Calcop = 0 Calcop = int(input("What operation do you want?\n1)+\n2)-\n3)x\n4)÷\n5)Mod\n6)Rounding\n7)Ceil\n8)Floor\n")) if Calcop == 1 or "+": Calcnumone = int(input("What is your first number?: ")) Calcnumtwo = int(input("What is your second number?: ")) print("\n") print("Your answer is...") print(Calcnumone + Calcnumtwo) print("\n") continue elif Calcop == 2 or "-": Calcnumone = int(input("What is your first number?: ")) Calcnumtwo = int(input("What is your second number?: ")) print("\n") print("Your answer is...") print(Calcnumone - Calcnumtwo) print("\n") continue elif Calcop == 3 or "x" or "*": Calcnumone = int(input("What is your first number?: ")) Calcnumtwo = int(input("What is your second number?: ")) print("\n") print("Your answer is...") print(Calcnumone * Calcnumtwo) print("\n") continue elif Calcop == 4 or "/": Calcnumone = int(input("What is your first number?: ")) Calcnumtwo = int(input("What is your second number?: ")) print("\n") print("Your answer is...") print(Calcnumone / Calcnumtwo) print("\n") continue elif Calcop == 5 or "%": Calcnumone = int(input("What is your first number?: ")) Calcnumtwo = int(input("What is your second number?: ")) print("\n") print("Your answer is...") print(Calcnumone % Calcnumtwo) print("\n") continue elif Calcop == 6 or "round": Calcnumone = int(input("What do you want to round?: ")) Calcnumtwo = round(Calcnumone) print(Calcnumtwo) continue elif Calcop == 7 or "ceil": Calcnumone = int(input("What do you want to ceil?: ")) Calcnumtwo = ceil(Calcnumone) print(Calcnumtwo) continue elif Calcop == 8: Calcnumone = int(input("What do you want to floor?: ")) Calcnumtwo = floor(Calcnumone) print(Calcnumtwo) continueI am looking to fix the bug, not to improve on the code any other way. Thanks in advance! RE: Calculator only does the + function - ichabod801 - Jul-13-2019 You need to have an expression on each side of the or. if Calcop == 1 or '+': is equivalent to if (Calcop == 1) or ('+'): . (see here for details)
RE: Calculator only does the + function - Clunk_Head - Jul-13-2019 (Jul-13-2019, 12:24 PM)ichabod801 Wrote: You need to have an expression on each side of the or. Also you are casting your input to an int Calcop = int(input("What operation do you want?\n1)+\n2)-\n3)x\n4)÷\n5)Mod\n6)Rounding\n7)Ceil\n8)Floor\n"))Since your input can be an it or a string, just leave it as a string and adjust your tests for strings Calcop = input("What operation do you want?\n1)+\n2)-\n3)x\n4)÷\n5)Mod\n6)Rounding\n7)Ceil\n8)Floor\n") if Calcop == '1' or Calcop == '+': RE: Calculator only does the + function - GalaxyCoyote - Jul-13-2019 note, this was part of a bigger piece, ignore some of the indenting errors |