我在练习21“艰难地学习Python”。在
以下是原始代码:
def add(a, b):
print "ADDING %d + %d" % (a, b)
return a + b
def subtract(a, b):
print "SUBTRACTING %d - %d" % (a, b)
return a - b
def multiply(a, b):
print "MULTIPLYING %d * %d" % (a, b)
return a * b
def divide(a, b):
print "DIVIDING %d / %d" % (a, b)
return a / b
print "Let's do some math with just functions!"
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
# A puzzle for the extra credit, type it in anyway.
print "Here is a puzzle."
what = add(age, subtract(height, multiply(weight, divide(iq, 2))))
print "That becomes: ", what, "Can you do it by hand?"
现在,我正试图通过创建一个新函数并返回使代码成为我自己的代码。在
这就是我要做的:
^{pr2}$我绝对没有错误。它询问有多少家餐馆和多少家杂货店,但最后就结束了。我错过什么了吗?我基本上是尝试从函数中的raw_input
和print
中获取值。我肯定我漏掉了一些显而易见的东西,但我想不出来。在
number_of_food = (number_restaurants, number_grocery)
通过上面的一行,您不是调用刚刚定义的函数。而是用元组覆盖它。首先,需要删除赋值运算符(
=
)。即使删除了它,它也不会打印函数返回的值。您还必须添加
print
关键字。在相关问题 更多 >
编程相关推荐