I have a simple calculator program in Python and I keep getting the NameError even though I already declared the function. And I am not sure why this is occurring. Also, I have another error at Line 25 of the code at the elif statement and it's showing me the invalid syntax error. However, I have checked the indentation and the elif statement and I'm not sure what is wrong with it. Thus, does anyone know what is the reason why this 2 errors occur and how can I fix them? Thank you.
Code:
'''
def add(first_num, second_num):
return float(first_num) float(second_num)
def subtract(first_num, second_num):
return float(first_num) - float(second_num)
def multiply(first_num, second_num):
return float(first_num) * float(second_num)
def divide(first_num, second_num):
return float(first_num) / float(second_num)
def calculator():
operator = input("Select an operation ( -*/): ")
first_num = input("Enter the first number: ")
second_num = input("Enter the second number: ")
if (operator == " ") :
result = add(first_num,second_num)
print(first_num, operator ,second_num, "=" ,result)
elif (operator == '-'): #invalid syntax error encountered here
result = subtract(first_num,second_num)
print(first_num, operator, second_num, "=", result)
elif (operator == '*'):
result = multiply(first_num,second_num)
print(first_num, operator, second_num, "=", result)
elif (operator == '-'):
result = divide(first_num,second_num)
print(first_num, operator, second_num, "=", result)
else:
print("Invalid operator entered")
'''
Screenshot of errors encountered:
- NameError when calling the calculator function
Screenshot of NameError
CodePudding user response:
Is it the complete code in file? Cause I cannot see where did you call the function inside the file.
You can do this thing:
- create main.py
- copy whole above code inside the file
- write 'calculate()' on last line
- go to terminal and write 'python main.py'
Your solution will start working.
CodePudding user response:
your code should be like this :
def add(first_num, second_num):
return float(first_num) float(second_num)
def subtract(first_num, second_num):
return float(first_num) - float(second_num)
def multiply(first_num, second_num):
return float(first_num) * float(second_num)
def divide(first_num, second_num):
return float(first_num) / float(second_num)
def calculator():
operator = input("Select an operation ( -*/): ")
first_num = input("Enter the first number: ")
second_num = input("Enter the second number: ")
if operator == " ":
result = add(first_num, second_num)
elif operator == '-': # invalid syntax error encountered here
result = subtract(first_num, second_num)
elif operator == '*':
result = multiply(first_num, second_num)
elif operator == '-':
result = divide(first_num, second_num)
else:
result = None
print("Invalid operator entered")
print(first_num, operator, second_num, "=", result)
OR
def add(first_num, second_num):
return float(first_num) float(second_num)
def subtract(first_num, second_num):
return float(first_num) - float(second_num)
def multiply(first_num, second_num):
return float(first_num) * float(second_num)
def divide(first_num, second_num):
return float(first_num) / float(second_num)
def calculator():
operator = input("Select an operation ( -*/): ")
first_num = input("Enter the first number: ")
second_num = input("Enter the second number: ")
if (operator == " "):
result = add(first_num, second_num)
print(first_num, operator, second_num, "=", result)
elif (operator == '-'): # invalid syntax error encountered here
result = subtract(first_num, second_num)
print(first_num, operator, second_num, "=", result)
elif (operator == '*'):
result = multiply(first_num, second_num)
print(first_num, operator, second_num, "=", result)
elif (operator == '-'):
result = divide(first_num, second_num)
print(first_num, operator, second_num, "=", result)
else:
print("Invalid operator entered")
You should not print any print between if and elif. If you need to print it, it should be in an if or elif block (in Python, blocks are expressed with carriage returns).