Python’添加’功能问题:为什么这不起作用?

Python’添加’功能问题:为什么这不起作用?,第1张

概述我刚刚开始学习 Python,我是一个绝对的新手. 我开始学习函数了,我写了这个简单的脚本: def add(a,b): return a + bprint "The first number you want to add?"a = raw_input("First no: ")print "What's the second number you want to add? 我刚刚开始学习 Python,我是一个绝对的新手.

我开始学习函数了,我写了这个简单的脚本:

def add(a,b):       return a + bprint "The first number you want to add?"a = raw_input("First no: ")print "What's the second number you want to add?"b = raw_input("Second no: ")result = add(a,b)print "The result is: %r." % result

脚本运行正常,但结果不是总和.即:如果我为’a’输入5,为’b’输入6,结果将不是’11’,而是56.如下:

The first number you want to add?First no: 5What's the second number you want to add?Second no: 6The result is: '56'.

任何帮助,将不胜感激.

解决方法 raw_input返回string,需要将其转换为int

def add(a,b):       return a + bprint "The first number you want to add?"a = int(raw_input("First no: "))print "What's the second number you want to add?"b = int(raw_input("Second no: "))result = add(a,b)print "The result is: %r." % result

输出:

The first number you want to add?First no: 5What's the second number you want to add?Second no: 6The result is: 11.
总结

以上是内存溢出为你收集整理的Python’添加’功能问题:为什么这不起作用?全部内容,希望文章能够帮你解决Python’添加’功能问题:为什么这不起作用?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1192814.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存