1

I am new to Python. I tried to make a simple calculator, but what is the problem?

def add(num1, num2):
 return num1 + num2
def subtract(num1, num2):
 return num1 - num2
def div(num1, num2):
 return num1/num2
def multi(num1,num2):
 return num1*num2
def main():
 operation = input("What do you want to do?(+, -, *, or /):")
 if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
 print("Your input is invalid. Please enter a valid input.")
 else:
 num1 = float(input("Enter value for num1: "))
 num2 = float(input("Enter value for num2: "))
 if (operation == "+"):
 print(add(num1, num2))
 elif (operation == "-"):
 print(subtract(num1, num2))
 elif (operation == "*"):
 print(multi(num1,num2))
 elif (operation == "/"):
 print(div(num1,num2))
 main()
Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Nov 5, 2021 at 9:20
1
  • 6
    main is inside the main function. It should be outside Commented Nov 5, 2021 at 9:21

3 Answers 3

3

You call main from inside itself. Set this outside the function like this:

def add(num1, num2):
 return num1 + num2
def subtract(num1, num2):
 return num1 - num2
def div(num1, num2):
 return num1/num2
def multi(num1,num2):
 return num1*num2
def main():
 operation = input("What do you want to do?(+, -, *, or /):")
 if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
 print("Your input is invalid. Please enter a valid input.")
 else:
 num1 = float(input("Enter value for num1: "))
 num2 = float(input("Enter value for num2: "))
 if (operation == "+"):
 print(add(num1, num2))
 elif (operation == "-"):
 print(subtract(num1, num2))
 elif (operation == "*"):
 print(multi(num1,num2))
 elif (operation == "/"):
 print(div(num1,num2))
main() # Added main outside the function
Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Nov 5, 2021 at 9:24
Sign up to request clarification or add additional context in comments.

Comments

1

Your main() has a Tab behind (before) it. It didn't run for me at first.

The other things seem fine to me.

You could also have it in a loop if you want to make it nicer.

def add(num1, num2):
 return num1 + num2
def subtract(num1, num2):
 return num1 - num2
def div(num1, num2):
 return num1/num2
def multi(num1,num2):
 return num1*num2
def main():
 operation = input("What do you want to do?(+, -, *, or /):")
 if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
 print("Your input is invalid. Please enter a valid input.")
 else:
 num1 = float(input("Enter value for num1: "))
 num2 = float(input("Enter value for num2: "))
 if (operation == "+"):
 print(add(num1, num2))
 elif (operation == "-"):
 print(subtract(num1, num2))
 elif (operation == "*"):
 print(multi(num1,num2))
 elif (operation == "/"):
 print(div(num1,num2))
if __name__ == '__main__':
 while(True):
 main()
 if input('If you are done with calculating, type q: ') == 'q':
 break
Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Nov 5, 2021 at 9:24

4 Comments

A tab afer main() does not matter and is not the issue.
afer? Do you mean 'after'? I said behind it, and it was the issue. :)
I'm pretty sure behind is closer to before not after.
I stand corrected that behind is closer to before, than after, but anyway, I edited my answer and used 'before' in the comments. You could also upvote me to help me to increase my reputation.
0

Simple calculator with very Easy steps in python. You are not calling the functions inside main.

# Defining Functions
def sum (x,y):
 return x+y
def div (x,y):
 return x/y
def sub (x,y):
 return x-y
def multi (x,y):
 return x*y
# getting Input for the operation to be performed
choice =input("Plesae enter choice:)\n'div' for Divide \n'multi' for Multiply\n'sub' for Subtraction\n'sum' for Addition\n")
#Performing opration According to the input of user
if(choice == "sum"):#condition
 x = int(input("Please enter number 1 :"))
 y = int(input("Please enter number 1 :"))
 ans = sum(x,y)
 print(f"your Answer is {x} + {y} = {ans} ")
 
 
elif(choice == "div"): #condition
 x = int(input("Please enter number 1 :"))
 y = int(input("Please enter number 1 :"))
 ans = div(x,y)
 print(f"your Answer is {x} + {y} = {ans} ")
 
 
 
 
elif(choice == "sub"):#condition
 x = int(input("Please enter number 1 :"))
 y = int(input("Please enter number 1 :"))
 ans = sub(x,y)
 print(f"your Answer is {x} + {y} = {ans} ") 
 
 
 
elif(choice == "multi"): #condition
 x = int(input("Please enter number 1 :"))
 y = int(input("Please enter number 1 :"))
 ans = multi(x,y)
 print(f"your Answer is {x} + {y} = {ans} ")
 
 
else: 
 print("Please Enter a Valid Number")
 
 
 
answered Sep 8, 2023 at 7:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.