Just began using python (have almost 0 clue)

40°
Deal Subedar
parikhmanaal156

def calculator(num1,num2,operator):
result = 0
if operator==“+”:
result=num1+num2
elif operator==“-”:
result=abs(num1-num2)
elif operator==“*”:
result=num1*num2
elif operator==“/”:
result= num1/num2
else:
print(“\n Invalid operator chosen”)
return result
print(“\n Welcome to calculator”)
print(“\n Enter two numbers followed by the operator”)
num1= input(“Enter number one”)
num2= input(“Enter number two”)
operator= input(“\n Enter operator”)
print(calculator(num1,num2,operator))

IF anyone knows python could they guide me where did I go wrong since I was trying basic stuff like making a calculator using function and return statement

6 Comments  |  
5 Dimers
  • Sort By
Deal Cadet Deal Cadet
Link Copied

What error you are getting??

Deal Newbie Deal Newbie
Link Copied

def calculator(num1,num2,operator):
result = 0
if operator==‘+’:
result=num1+num2
elif operator==‘-’:
result=abs(num1-num2)
elif operator==‘*’:
result=num1*num2
elif operator==‘/’:
result= num1/num2
else:
print(‘\n Invalid operator chosen’)
return result
print(‘\n Welcome to calculator’)
print(‘\n Enter two numbers followed by the operator’)
num1= int(input(‘Enter number one’))
num2= int(input(‘Enter number two’))
operator= input(‘\n Enter operator’)
print(calculator(num1,num2,operator))

Deal Newbie Deal Newbie
Link Copied

num1= int(input(‘Enter number one’))
num2= int(input(‘Enter number two’))
here you have to write int otherwise it take string type input

Deal Captain Deal Captain
Link Copied

you are not converting num1 and num2 to int before passing them to calculator method.
ref: https://ideone.com/...e4

Deal Subedar Deal Subedar
Link Copied

Thankyou and the other problem I had was I couldn’t understand the difference between range vs xrange…

replyuser
Click here to reply
Reply