TypeError: 'str' object cannot be interpreted as an integer even though I assigned it with int(value)

有些话、适合烂在心里 提交于 2020-01-25 08:17:09

问题


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!")

I get the following error message:

>>> 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
>>> 


I am using python 3.8

EDIT:

_>>> 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
''
>>> _

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


回答1:


Hello as the error tells you:

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

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

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("")

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.



来源:https://stackoverflow.com/questions/59084229/typeerror-str-object-cannot-be-interpreted-as-an-integer-even-though-i-assign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!