Python 不明白为什么我得到一个“int”对象是不可容忍的错误
不明白为什么会出现TypeError:“int”对象是不可编辑的Python 不明白为什么我得到一个“int”对象是不可容忍的错误,python,Python,不明白为什么会出现TypeError:“int”对象是不可编辑的 first_age = int(input("Enter your age: ")) second_age = int(input("Enter your age: ")) total = sum(first_age, second_age) print("Together you're {} years old".format(total)) 输入您的年龄:1 输入您的年龄:1 ------------------------
first_age = int(input("Enter your age: "))
second_age = int(input("Enter your age: "))
total = sum(first_age, second_age)
print("Together you're {} years old".format(total))
输入您的年龄:1
输入您的年龄:1
--------------------------------------
TypeError回溯最近一次呼叫last
在里面
4第二个年龄=输入您的年龄:
5.
-->6总计=第一个年龄,第二个年龄
7.
8.你已经{}岁了
TypeError:“int”对象不可编辑
TypeError回溯最近一次呼叫last
在里面
4第二个年龄=输入您的年龄:
5.
-->6总计=第一个年龄,第二个年龄
7.
8.你已经{}岁了
TypeError:“int”对象不可编辑
sum只能用于可迭代对象。见官员
语法本身是
sum(iterable[, start])
如果您只需要对两个整数求和,请使用下面代码中的+运算符
sum = first_age + second_age
sum只能用于可迭代对象。见官员
语法本身是
sum(iterable[, start])
如果您只需要对两个整数求和,请使用下面代码中的+运算符
sum = first_age + second_age
函数sum用于对可迭代项求和。因此,如果您输入sum[7,8],您将返回15。这就是为什么会出现错误,因为它试图迭代整数类型而不是数组
解决方案如下:
first_age = int(input("Enter your age: "))
second_age = int(input("Enter your age: "))
total = sum([first_age,second_age])
print(f"Together you're {total} years old.")
函数sum用于对可迭代项求和。因此,如果您输入sum[7,8],您将返回15。这就是为什么会出现错误,因为它试图迭代整数类型而不是数组
解决方案如下:
first_age = int(input("Enter your age: "))
second_age = int(input("Enter your age: "))
total = sum([first_age,second_age])
print(f"Together you're {total} years old.")
你不想使用
根据文件:
sum(iterable[, start])
将iterable的起始项和项从左到右求和,并返回总数。开始默认为0。iterable的项通常是数字,并且起始值不允许是字符串
您只需要使用加法
first_age = int(input("Enter your age: "))
second_age = int(input("Enter your age: "))
total = first_age + second_age
print("Together you're {} years old".format(total))
你不想使用
根据文件:
sum(iterable[, start])
将iterable的起始项和项从左到右求和,并返回总数。开始默认为0。iterable的项通常是数字,并且起始值不允许是字符串
您只需要使用加法
first_age = int(input("Enter your age: "))
second_age = int(input("Enter your age: "))
total = first_age + second_age
print("Together you're {} years old".format(total))
为什么会被否决?这是正确答案。您确定要在此处使用列表吗?@CodeIt此处仅使用普通加法更有意义,但我相信op正在寻找如何使用求和函数的方向。@JDPellock这是正确的。问题不在于如何添加整数?这个问题是关于当sum与传递给它的两个整数一起使用时发生的错误。@CodeIt这是真的。JD确实使用了错误的术语。我已经申请了编辑。为什么这被否决了?这是正确答案。您确定要在此处使用列表吗?@CodeIt此处仅使用普通加法更有意义,但我相信op正在寻找如何使用求和函数的方向。@JDPellock这是正确的。问题不在于如何添加整数?这个问题是关于当sum与传递给它的两个整数一起使用时发生的错误。@CodeIt这是真的。JD确实使用了错误的术语。我已经应用了编辑。虽然这是绝对正确的,但它实际上并没有回答问题。添加了编辑,解释应该在哪里使用求和函数。虽然这是绝对正确的,但它实际上并没有回答问题。添加了编辑,解释应该在哪里使用求和函数。