Just began using python (have almost 0 clue)
- 427
- 7
-
- Last Comment
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
- Sort By
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))
num1= int(input(‘Enter number one’))
num2= int(input(‘Enter number two’))
here you have to write int otherwise it take string type input
you are not converting num1 and num2 to int before passing them to calculator method.
ref: https://ideone.com/...e4
Thankyou and the other problem I had was I couldn’t understand the difference between range vs xrange…
if you are learning python, learn pydoc
https://stackoverflow.com/questions/21591572/in...
What error you are getting??