Can't get an input into a range

I’ve been playing around with trying to make a range from an input.
But can’t get it to work despite others on the internet says it should work.
What am I doing wrong?


start = int(input("whats the end of the range? "))
range = []

for i in range(1, start):
    print(f"first number is: {i}")
    range.append(i)

print("numbers are now: ", range)

However terminal is giving me the

File “ex33.py”, line 38, in
for i in range(1, start):
TypeError: ‘list’ object is not callable

Hi @ktrager it will work. The only thing that you have to change is the name of the variable range = []. You call it like the Python built-in function range(). So Python will mistakenly call that function instead of your global variable.

Change range = [] to something else like v_range = [] and it will work.

start = int(input("whats the end of the range? "))
v_range = []

for i in range(1, start):
    print(f"first number is: {i}")
    v_range.append(i)

print("numbers are now: ", v_range)

But attention: you can only input a number!

$ python3.6 test.py 
whats the end of the range? 3
first number is: 1
first number is: 2
numbers are now:  [1, 2]

You can make a test:
Write the following in your code:

print(range)

And watch the output carefully. It will tell you that range is of type <class 'range'>

3 Likes

As @DidierCH says, range is a python keyword/builtin so avoid using it as a variable or object unless you want a headache (or are extending it)

https://docs.python.org/3.7/library/functions.html

1 Like

The worst mistake…
Not sure why I thought range on it’s own was going to be a good name for a list.

Thanks for clarifying. Spend a good 30min not spotting the very very obvious…

Hi @ktrager don’t worry. I don’t think it’s obvious to a beginner. I already spent longer periods of time, sometimes days, to locate an error similar to this.

1 Like

That’s totally python’s fault @ktrager, not yours. Python has so many global variables that are common names that you run into this all the time.

2 Likes