Python Forum
New to python, trying to make a basic calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New to python, trying to make a basic calculator
#1
Hey, so I've got this piece of code that I put together that is pretty simple. However, when I execute the code it executes all the if statements instead of one. I know it has to be something wrong with my if statements, but I don't know what. Changing it to an elif doesn't help me. Any help would be appreciated. Thanks!

first_number = input("First Number: ")
first_number = int(first_number)

operator = input("Operation: ")

second_number = input("Second Number: ")
second_number = int(second_number)


for sign in operator:
	if sign == "*" or "x" or "multiply" or "times":
		print(first_number * second_number)
	if sign == "/" or "divide":
		print(first_number / second_number)
	if sign == "+" or "plus" or "add":
		print(first_number + second_number)
	if sign == "-" or "minus" or "subtract":
		print(first_number - second_number)
Also, if there's an easier solution to what I've made please let me know. Thanks!
Reply
#2
Check https://python-forum.io/thread-121.html
Also better use if/elif instead of multiple separate ifs
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Apr-14-2022, 10:17 AM)buran Wrote: Check https://python-forum.io/thread-121.html
Also better use if/elif instead of multiple separate ifs

Awesome, thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Calculator Application MemeStealer 7 43,284 Apr-17-2025, 07:02 PM
Last Post: AndreyAD
  Need some help trying to get my Python mortgage amortization calculator to work prope IamSirAskAlot 4 17,347 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  Very basic calculator question BoudewijnFunke 4 2,908 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  How To Create A "Birthday Calculator" in Python? unigueco9 3 6,591 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  python calculator only using decomposing functions kirt6405 1 2,417 Jun-19-2021, 12:52 AM
Last Post: bowlofred
  List index out of range error when attempting to make a basic shift code djwilson0495 4 4,245 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Python calculator divide by zero help dock1926 4 8,063 Jan-20-2020, 05:15 PM
Last Post: michael1789
  Python Program to Make a Simple Calculator jack_sparrow007 2 12,461 Oct-19-2018, 08:32 AM
Last Post: volcano63
  Creating a Calculator with Python KatherineHov 8 9,498 Aug-03-2017, 02:13 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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