Answer the question
In order to leave comments, you need to log in
How to return (return) value in def?
Good day, please tell me what is wrong with the return of the "result" value and what needs to be corrected in the code so as not to make such a mistake in the future?
# Калькулятор Def
print("Вас привествует примитивный калькулятор!!! \n")
number_1 = int (input("Введите первое число "))
znak = input ("Введите знак того, что сделать с этим числом ")
number_2 = int (input("Введите второе число "))
def calculator (number_1, number_2, znak):
if znak.lower == "*":
result = number_1 * number_2
elif znak.lower == "-":
result = number_1 - number_2
elif znak.lower == "+":
result = number_1 + number_2
elif znak.lower == "/":
result = number_1 / number_2
return result
calculator(number_1, number_2, znak)
Вас привествует примитивный калькулятор!!!
Введите первое число 23
Введите знак того, что сделать с этим числом -
Введите второе число 33
Traceback (most recent call last):
File "C:\python\test222222222222.py", line 28, in <module>
calculator(number_1, number_2, znak)
File "C:\python\test222222222222.py", line 25, in calculator
return result
UnboundLocalError: local variable 'result' referenced before assignment
Answer the question
In order to leave comments, you need to log in
In a particular case, I see no reason to create a variable.
You can return a value without declaring a variable.
elif znak = '/' and number_2:
return number_1 / number_2
else:
return 'ОШИБКА'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question