python数字求和为什么得不出结果_Python不求和(加)数字,只把它们粘在一起

所以我刚刚开始学习如何编写代码(这是全新的),我决定使用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?"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值