TypeError:'str'对象不能解释为整数,即使我为它分配了int(value)

问题描述 投票:0回答:1

当我输入简单的代码时:

times = input("How many times do I have to tell you? ")
times = int(times)

for i in range(times):
    print("Clean your room!")

我收到以下错误消息:

>>> times = input("How many times do I have to tell you? ")
How many times do I have to tell you? times = int(times)
>>> 
>>> for time in range(times):
...     print("Clean your room!")
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>> 


我正在使用python 3.8

int
1个回答
0
投票

您好,因为错误告诉您:

TypeError: 'str' object cannot be interpreted as an integer

首先您要在第1行中添加字符串。在第2行中,您需要对一个int进行设置。

因此更改第一行:

question = input("How many times do I have to tell you? ")
© www.soinside.com 2019 - 2024. All rights reserved.