我正在学习 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()
添加回答
举报
0/150
提交
取消