为了账号安全,请及时绑定邮箱和手机立即绑定

值错误:值太多

值错误:值太多

守着一只汪 2021-08-14 12:52:42
我正在学习 python 教程。我完全输入了教程中的内容,但它无法运行。我认为问题是教程使用了 Python 2,而我使用的是 Python 3.5。例如,教程在打印后不使用括号,我必须使用,它使用 raw_input,而我只使用输入。这就是我想要运行的-def sumProblem(x, y):     print ('The sum of %s and %s is %s.' % (x, y, x+y))def main():     sumProblem(2, 3)     sumProblem(1234567890123, 535790269358)     a, b = input("Enter two comma separated numbers: ")     sumProblem(a, b)main()这是我收到的错误:ValueError: too many values to unpack (expected 2)如果我只输入两个没有逗号的数字,它将连接它们。我试图更改为整数,但它给出了这个错误:ValueError: invalid literal for int() with base 10: 当我在这里搜索时,答案似乎不适用于我的问题,他们涉及更多或我不明白。
查看完整描述

2 回答

?
红颜莎娜

TA贡献1842条经验 获得超12个赞

input(..)返回一个字符串。字符串是可迭代的,因此您可以使用以下命令解压缩:


a, b = input("Enter two comma separated numbers: ") 

但前提是字符串恰好包含两个项目。因此,对于字符串,这意味着该字符串恰好包含两个字符。


然而,代码提示您要输入两个整数。我们可以使用str.split()将字符串拆分为“单词”列表。


然后我们可以map使用intas 函数执行ping :


def sumProblem(x, y): 

    print ('The sum of %s and %s is %s.' % (x, y, x+y))

def main(): 

    sumProblem(2, 3) 

    sumProblem(1234567890123, 535790269358) 

    a, b = map(int, input("Enter two comma separated numbers: ").split(','))

    sumProblem(a, b)

main()


查看完整回答
反对 回复 2021-08-14
  • 2 回答
  • 0 关注
  • 149 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号