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
-
6main is inside the main function. It should be outsidealparslan mimaroğlu– alparslan mimaroğlu2021年11月05日 09:21:55 +00:00Commented Nov 5, 2021 at 9:21
3 Answers 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
Sign up to request clarification or add additional context in comments.
Comments
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
4 Comments
luk2302
A tab afer
main() does not matter and is not the issue.Yousef
afer? Do you mean 'after'? I said behind it, and it was the issue. :)
Yousef
I'm pretty sure behind is closer to before not after.
Yousef
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.
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")
Comments
lang-py