首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:“str”对象不能解释为整数,即使我将它赋值为int(value)

TypeError:“str”对象不能解释为整数,即使我将它赋值为int(value)
EN

Stack Overflow用户
提问于 2019-11-28 16:07:37
回答 1查看 65关注 0票数 0

当我输入我的简单代码时:

代码语言:javascript
运行
复制
times = input("How many times do I have to tell you? ")
times = int(times)

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

我得到以下错误消息:

代码语言:javascript
运行
复制
>>> 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

编辑:

代码语言:javascript
运行
复制
_>>> times = int(input("How many times do I have to tell you? "))
How many times do I have to tell you? 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
>>> for i in range(times):
...     print("Clean your room!")
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'times' is not defined
>>> input("2")
2
''
>>> _

我想我的.bashrc和.bash_profile出了点问题。

EN

回答 1

Stack Overflow用户

发布于 2019-11-28 16:11:43

您好,因为错误告诉您:

代码语言:javascript
运行
复制
TypeError: 'str' object cannot be interpreted as an integer

首先你在第一行输入字符串,在第二行输入一个int。

我再检查一次,我可以告诉你这两个版本都是100%有效的:

代码语言:javascript
运行
复制
times = input("How many times do I have to tell you? ")
times = int(times)

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

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

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

input("")

我在最后添加了input(""),这样控制台就可以一直打开

如果这仍然不起作用,我们需要寻找其他地方。但我相信我们会想出办法的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59084229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档