0

I am creating a function that should return a list of every third number between start and 100 (inclusive). For example, every_three_nums(91) should return the list [91, 94, 97, 100]. Initially, I tried the following:

def every_three_nums(start):
  list = []
  a = 0
  if start > 100:
    return list
  else:
    a = range(start, 101, 3)
    print(list(a))    

print(every_three_nums(91))

And I get TypeError: 'list' object is not callable.

The same happens if I use

print(list(range(91, 101, 3)))

within the function. Despite this working outside the function.

I don't quite understand the rationale. Could you help me with this?

Eventually, I solved with:

def every_three_nums(start):
  list = []
  a = 0
  if start > 100:
    return list
  else:
    for i in range(start, 101, 3):
      list.append(i)
    return list

print(every_three_nums(91))
1
  • In the first code, you declare list as a variable in the first code and then you call the list after some time. Commented Apr 16, 2020 at 17:15

1 Answer 1

1

You are redefining the list function to a list instance inside the function with list = []. So when you call list(a), you are treating a list object as a function. Rename that variable and it should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.