我如何将 str 转换为 int?
How would I convert a str to an int?
# counter
my_list = input("Input your list.(Numbers)\n")
while my_list.isnumeric() == False:
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n")
else:
counter = 0
for item in my_list:
counter_item = counter + item
print(int(counter_item))
确切的错误信息是
"Traceback (most recent call last):
File "main.py", line 13, in <module>
counter_item = counter + item enter code here
TypeError: unsupported operand type(s) for +: 'int' and 'str'"
我该怎么办?
P.S(在 Repl.it 上的 Python3 中)
使用 .isdigit()
而不是 .isnumeric()
,检查输入是否只是数字。
my_list = input("Input your list.(Numbers)\n")
while not my_list.isdigit():
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n")
else:
counter = 0
for item in my_list:
counter +=int(item)
print(counter)
另外,使用+=
。它会添加值而不是每次都创建一个局部变量。
错误TypeError: unsupported operand type(s) for +: 'int' and 'str'"
指出它不能添加字符串和整数。
+
2 个字符串之间用于字符串连接。
+
2个整数之间为加法。
你可能想做的是第二个。
所以将 s
转换为整数
该错误表明您正在添加 str
和 int
。在添加计数器之前,您需要将 item
转换为 int
。
counter_item = counter + int(item)
您需要先将字符串转换为整数(使用 int
函数),然后才能添加它们。此外,如果您的意图是让用户输入多个数字,您可能希望在将数字转换为整数之前 split()
字符串——这意味着您不能在原始字符串上使用 isnumeric()
检查输入错误,因为它会包含空格!
这是我的做法:
while True:
try:
print(sum(map(
int,
input("Input your list.(Numbers)\n").split()
)))
break
except ValueError:
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
这是异常作为错误处理机制的强大功能的一个很好的例子——而不是必须单独检查列表的每个元素(就像您在不支持异常的语言中必须做的那样) ,您可以在假设一切正常的情况下对列表进行操作,并使用单个 try/except
来捕获结果 ValueErrors
,无论它们在该过程中的哪个位置被引发。将整个事情放在 while
循环中可以让您简单地重试,直到整个操作序列能够无一例外地完成。
示例输出:
Input your list.(Numbers)
lskdjf
----------------------------------------------------
***Sorry, what you had entered contained a letter.***
----------------------------------------------------
Input your list.(Numbers)
1 2 3 4
10
首先,使用split()
将输入拆分为列表。
然后,您需要对列表的每个元素而不是整个字符串进行测试 isnumeric()
。
调用int()
将列表元素转换为整数。
最后,将总和加到 counter
,而不是 counter_item
。
my_list = input("Input your list.(Numbers)\n").split()
while not all(item.isnumeric() for item in my_list):
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n").split()
counter = 0
for item in my_list:
counter = counter + int(item)
print(counter)
你不需要在循环后使用else:
。仅当循环可以使用 break
结束并且您希望仅在循环正常结束时才 运行 编码时才需要这样做。
打印时不需要使用int()
,因为counter
已经是整数了。
while True:
input_lst = map(int,input("Enter list of number").split())
try:
counter = 0
for val in input_lst:
counter += int(val)
break
except Exception:
print ("All the inputs were not numeric.")
print (counter)
你的输入方式错误,输入将一个 sring 写入 My_list。
my_list = input("Input your list.(Numbers)\n").split(" ")
isnumaric 函数适用于整数数据类型,但不适用于 arry。
所以你需要应用一个for循环或while循环来检查arry上的每个元素。
counter 是整数数据类型,item 是字符串数据类型,因为输入函数返回字符串,所以您需要使用 int()
function 将字符串转换为 int。
counter_item = counter_item + int(item)
我已经制作了一个可能适合您的代码。
my_list = input("Input your list.(Numbers)\n").split(" ")
print(my_list)
i =0
while i!= len(my_list):
for item in my_list:
if item.isnumeric():
i+=1
continue
else:
i=0
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n").split(" ")
break
else:
counter_item = 0
for item in my_list:
counter_item = counter_item + int(item)
print(int(counter_item))
# counter
my_list = input("Input your list.(Numbers)\n")
while my_list.isnumeric() == False:
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n")
else:
counter = 0
for item in my_list:
counter_item = counter + item
print(int(counter_item))
确切的错误信息是
"Traceback (most recent call last):
File "main.py", line 13, in <module>
counter_item = counter + item enter code here
TypeError: unsupported operand type(s) for +: 'int' and 'str'"
我该怎么办?
P.S(在 Repl.it 上的 Python3 中)
使用 .isdigit()
而不是 .isnumeric()
,检查输入是否只是数字。
my_list = input("Input your list.(Numbers)\n")
while not my_list.isdigit():
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n")
else:
counter = 0
for item in my_list:
counter +=int(item)
print(counter)
另外,使用+=
。它会添加值而不是每次都创建一个局部变量。
错误TypeError: unsupported operand type(s) for +: 'int' and 'str'"
指出它不能添加字符串和整数。
+
2 个字符串之间用于字符串连接。+
2个整数之间为加法。
你可能想做的是第二个。
所以将 s
转换为整数
该错误表明您正在添加 str
和 int
。在添加计数器之前,您需要将 item
转换为 int
。
counter_item = counter + int(item)
您需要先将字符串转换为整数(使用 int
函数),然后才能添加它们。此外,如果您的意图是让用户输入多个数字,您可能希望在将数字转换为整数之前 split()
字符串——这意味着您不能在原始字符串上使用 isnumeric()
检查输入错误,因为它会包含空格!
这是我的做法:
while True:
try:
print(sum(map(
int,
input("Input your list.(Numbers)\n").split()
)))
break
except ValueError:
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
这是异常作为错误处理机制的强大功能的一个很好的例子——而不是必须单独检查列表的每个元素(就像您在不支持异常的语言中必须做的那样) ,您可以在假设一切正常的情况下对列表进行操作,并使用单个 try/except
来捕获结果 ValueErrors
,无论它们在该过程中的哪个位置被引发。将整个事情放在 while
循环中可以让您简单地重试,直到整个操作序列能够无一例外地完成。
示例输出:
Input your list.(Numbers)
lskdjf
----------------------------------------------------
***Sorry, what you had entered contained a letter.***
----------------------------------------------------
Input your list.(Numbers)
1 2 3 4
10
首先,使用split()
将输入拆分为列表。
然后,您需要对列表的每个元素而不是整个字符串进行测试 isnumeric()
。
调用int()
将列表元素转换为整数。
最后,将总和加到 counter
,而不是 counter_item
。
my_list = input("Input your list.(Numbers)\n").split()
while not all(item.isnumeric() for item in my_list):
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n").split()
counter = 0
for item in my_list:
counter = counter + int(item)
print(counter)
你不需要在循环后使用else:
。仅当循环可以使用 break
结束并且您希望仅在循环正常结束时才 运行 编码时才需要这样做。
打印时不需要使用int()
,因为counter
已经是整数了。
while True:
input_lst = map(int,input("Enter list of number").split())
try:
counter = 0
for val in input_lst:
counter += int(val)
break
except Exception:
print ("All the inputs were not numeric.")
print (counter)
你的输入方式错误,输入将一个 sring 写入 My_list。
my_list = input("Input your list.(Numbers)\n").split(" ")
isnumaric 函数适用于整数数据类型,但不适用于 arry。 所以你需要应用一个for循环或while循环来检查arry上的每个元素。
counter 是整数数据类型,item 是字符串数据类型,因为输入函数返回字符串,所以您需要使用 int()
function 将字符串转换为 int。
counter_item = counter_item + int(item)
我已经制作了一个可能适合您的代码。
my_list = input("Input your list.(Numbers)\n").split(" ")
print(my_list)
i =0
while i!= len(my_list):
for item in my_list:
if item.isnumeric():
i+=1
continue
else:
i=0
print("\n----------------------------------------------------")
print("***Sorry, what you had entered contained a letter.***")
print("----------------------------------------------------\n")
my_list = input("Input your list.(Numbers)\n").split(" ")
break
else:
counter_item = 0
for item in my_list:
counter_item = counter_item + int(item)
print(int(counter_item))