所以我刚刚开始学习如何编写代码(这是全新的),我决定使用Python。。。所以我最近正在学习如何使用函数来做数学,我正在做我自己的“编码”,看看我能不能得出我想要的结果,那就是使用函数来加x+y,并给我一个结果,但我一直得到字面上的x+y,而不是这两个数字的和。例1+1=11(代替2)
下面是密码,谁能告诉我我做错了什么。谢谢!~
(是的,我用的是一本书,但在解释上有些含糊不清[艰难地学习Python])def add(a, b):
print "adding all items"
return a + b
fruits = raw_input("Please write the number of fruits you have \n> ")
beverages = raw_input("Please write the number of beverages you have \n> ")
all_items = add(fruits, beverages)
print all_items
仅供参考,这本书给我的代码是: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)
# puzzle
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?"