{TypeError:不支持使用inpu的+:“int”和“str”}的操作数类型

2024-05-19 07:23:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python的新手,我正和Grok一起学习,但我在这方面遇到了麻烦

money = input("Enter the expenses: ")
addup = money.split()
total = sum(addup)
print("Total: ", total)

Tags: theinputgroktotalsplitsumprintenter
2条回答

你在试图“求和”字符串,这当然行不通。你需要使它们成为整数或浮点数来相加。具体怎么做取决于你期望用户输入什么。在

这可能有用

addup=[float(x) for x in addup]

你需要做的是:

money=map(float,input().split()) #could have use int as well if input was a sequence of integers
total=sum(money)
print("Total:",total)

你也可以这样做:

^{pr2}$

在python中,输入默认为字符串,您需要对其进行类型转换

int(input("enter the expenses should work: "))

或者是浮动的

float(input("enter the expenses should work: "))

一般来说,你所需要的就是排版或者如果你的输入是一个数字列表的话

map(int,input().split()) #can use float here as well 

在python2.x中,map将创建一个列表,而在python3.x中,map将创建生成器。在

相关问题 更多 >

    热门问题