+ 1
I want to make a calculator by definining function. Can you help me to fix the problem?
3 Respostas
+ 10
Your code was poorly formatted and indented, which is intolerable in python.
Correct code:
a = input()
x = int(input())
y = int(input())
def sum(a,b):
p = (a+b)
print(p)
def substract(a,b):
p = (a-b)
print(p)
def division(a,b):
if b != 0:
p = (a/b)
print(p)
def multiply(a,b):
p = (a*b)
print(p)
if a == "sum":
sum(x,y)
elif a == "substract":
substract(x,y)
elif a == "multiply":
multiply(x,y)
elif a == "division":
division(x,y)
else:
print("not available")
+ 6
You must first defining function and then call it, like this:
def sum(a,b):
p=(a+b)
print (p)
def substract(a,b):
p=(a-b)
print (p)
def division (a,b):
if b!=0:
p= (a/b)
print (p)
def multiply(a,b):
p=(a*b)
print (p)
a= input ()
x=int(input ())
y= int(input ())
if a =="sum":
sum(x,y)
elif a=="substract":
substract(x,y)
elif a=="multiply":
multiply(x,y)
elif a=="division":
division(x,y)
else:
print ("not available")
0
thank you all.