我正在用Python学习函数,我被要求编写一个脚本,接受两个输入值并对它们执行一些数学函数。我写了下面的代码,但一直收到错误消息,在我试图打印答案的第17行,没有定义“result”。我不理解这一点,因为我觉得我在每个函数中定义“结果”。显然,我遗漏了一些与函数和返回值相关的基本概念。任何帮助都将不胜感激。在def sum(a,b):
result = a + b
return result
def times(a,b):
result = a * b
return result
def divide(a,b):
result = a / b
return result
def subtract(a,b):
result = a / b
return result
print "Answer is %d" % result
def start():
print "This program can perfom a math function of any two numbers"
a = int(raw_input("Enter first number: "))
b = int(raw_input("Enter second number: "))
c = raw_input("Enter math function you want: ")
if c == "+":
sum(a,b)
elif c == "x":
times(a,b)
elif c == "/":
divide(a,b)
elif c == "-":
subtract(a,b)
else:
print "you didnt enter a function!"
start()
错误如下:
文件“defPrac2.py”,第17行,输入
打印“答案是%d%”结果