TypeError:'str'对象不能解释为整数,即使我将其分配给int(value) [英] TypeError: 'str' object cannot be interpreted as an integer even though I assigned it with int(value)

查看:267
本文介绍了TypeError:'str'对象不能解释为整数,即使我将其分配给int(value)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我输入简单的代码时:

When I type in my simple code:

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

I am using python 3.8

_>>> 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有问题.

I think I am having some problems with my .bashrc and .bash_profile.

推荐答案

您好,错误提示:

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

首先在第1行中添加字符串. 在第2行中,您将对一个int进行了评估.

first you assing string in line 1. In line 2 you assing an int.

我再次检查了一下,我可以说两个版本都可以达到100%:

I checked it again and i can tell both versions work to 100%:

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(""),以便控制台保持打开状态

i added the input("") at the end so the console keeps open

如果这仍然行不通,我们需要寻找其他地方.但我敢肯定,我们会解决这个问题.

if this still wont work we Need to look somewhere else. But I´m sure we will figure this out.

这篇关于TypeError:'str'对象不能解释为整数,即使我将其分配给int(value)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆