1

I'm working on a script in Python but I have a problem: The for loop is supposed to run code in the loop until it reaches the variable 'leng'. Because for loops stop at right before the stop value is reached, I need the stop value to be 1 more than leng. I have a script that looks something like this:

leng = input()
for i in range(1, (leng + 1)):

but I always get the error message in the shell:

t_Creator_v1.py", line 9, in <module>
for i in range (1, (leng + 1)):
TypeError: Can't convert 'int' object to str implicitly

I tried searching stackoverflow but I couldn't find any results that suit me. How would I get what result I want? Please respond, thanks.

1 Answers1

2

Your taking in just normal input from the user which returns as a string representation of the data they enter. You need to parse it into an int if you want to do what you're trying.

leng = int(input())
for i in range(1, (leng + 1)):
m_callens
  • 6,100
  • 8
  • 32
  • 54