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