In addition, blanks and tabs look the same, but Python can see the difference, so even if you can't see an indentation error, it could still be there.
I've got your code working. Note that the following line was wrong:
if(operation == "yes please" and operation == "please" and operation == "yes" and operation == "yeah" and operation == "yup" and operation == "yep"):
If you wanted to do this, you'd need to use "or" not "and". Simpler to use "if operation in ...,...,"
Also note that "input" should be replaced by "raw_input" - see code. Good Luck!
def add(num1, num2):
return num1 + num2
def sub(num1, num2):
return num1 - num2
def mul(num1, num2):
return num1 * num2
def div(num1, num2):
return num1 / num2
def main():
operation = raw_input("What do you want to do? (+,-,*,/) ")
if operation not in {'+','-','*','/'}:
#invalid operation
print("You must enter a valid operation")
return main()
else:
var1 = int(input("Enter the first number: "))
var2 = int(input("Enter the second number: "))
if(operation == '+'):
print(add(var1, var2))
elif(operation == '-'):
print(sub(var1, var2))
elif(operation == '*'):
print(mul(var1, var2))
else:
print(div(var1, var2))
operation = raw_input("Would you like to exit? ")
if operation in {"yes please", "please", "yes", "yeah", "yup", "yep"}:
exit
else:
return main()
main()
I'm just starting to learn to code today, and am starting with Python (though I'm really wanting to start C++ soon, if not by the end of the day). My second code so far is a calculator that does not turn off after one calculation, but i can't figure out what to do about the syntax error. It says that "'return' outside function".
Here's my code:
def add(num1, num2):
return num1 + num2
def sub(num1, num2):
return num1 - num2
def mul(num1, num2):
return num1 * num2
def div(num1, num2):
return num1 / num2
def main():
operation = input("What do you want to do? (+,-,*,/) ")
if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):
#invalid operation
print("You must enter a valid operation")
return main()
else:
var1 = int(input("Enter the first number: "))
var2 = int(input("Enter the second number: "))
if(operation == '+'):
print(add(var1, var2))
elif(operation == '-'):
print(sub(var1, var2))
elif(operation == '*'):
print(mul(var1, var2))
else:
print(div(var1, var2))
operation = input("Would you like to exit?")
if(operation == "yes please" and operation == "please" and operation == "yes" and operation == "yeah" and operation == "yup" and operation == "yep"):
exit
elif(operation == "no" and operation == "nope" and operation == "no thanks" and operation == "no thank you"):
return main()
main()