小编Eri*_*Ahn的帖子

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
Run Code Online (Sandbox Code Playgroud)

仅供参考,本书给我的代码是:

    def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return …
Run Code Online (Sandbox Code Playgroud)

python

4
推荐指数
1
解决办法
4481
查看次数

标签 统计

python ×1