<分区>
所以我刚开始学习如何编码(在这方面是全新的)并且我决定使用 Python...所以我最近正在学习如何使用函数来做数学并且我正在制作自己的“编码”以查看如果我能得出我想要的结果,那就是使用函数来添加 x + y 并给我一个结果,但我一直得到文字 x + y 而不是这两个数字的总和。例如。 1 + 1 = 11(而不是 2)
下面是代码,谁能告诉我我做错了什么。谢谢!~ (是的,我正在使用一本书,但它的解释有些含糊 [Learn Python the Hard Way])
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?"