-1

I am new to Python and I wonder what is wrong with this code, I searched for this,I guess in the end of the make_list function I have to return, and not print(). and in the end I have to write if name[0] and not if name[1], but what else is wrong with it?

Code:

def make_list(number):
    names = []
    for item in number:
        names.append(input("Enter your name with a capital letter."))
        print(names)
        
number = int(input("How many names need to be entered?"))
names = make_list(number)
for name in names:
    if name [1] == 'A':
        print("Name", name, "starts with A")

Compiler says:

Traceback (most recent call last):
File "<string>", line 8, in <module>
File "<string>", line 3, in make_list
TypeError: 'int' object is not iterable
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • `number` is an int, so `for item in number` does not make sense. Also `make_list` doesn't return anything. – khelwood Feb 10 '22 at 09:55
  • 1
    Should be `for item in range(number):` – Maurice Meyer Feb 10 '22 at 09:56
  • 4
    Python doesn't know what e.g. `for item in 3` is supposed to mean. In for loop, you need some kind of iterable, so e.g. [0, 1, 2], which you can get by using `range(3)` – matszwecja Feb 10 '22 at 09:56
  • Potential Duplicate: https://stackoverflow.com/questions/14941288/how-do-i-fix-typeerror-int-object-is-not-iterable – Liam May 08 '22 at 05:24
  • Does this answer your question? [How do I fix TypeError: 'int' object is not iterable?](https://stackoverflow.com/questions/14941288/how-do-i-fix-typeerror-int-object-is-not-iterable) – Liam May 08 '22 at 05:24

5 Answers5

1

You need to have range(numbers) to iterate, and you also forgot to return names from the make_list function. Also, as the arrays in Python are zero-indexed, you probably want to check if name [0]

def make_list(number):
    names = []
    for item in range(number):
        names.append(input("Enter your name with a capital letter."))
        print(names)
    return names
        
number = int(input("How many names need to be entered?"))
names = make_list(number)
for name in names:
    if name [0] == 'A':
        print("Name", name, "starts with A")
user23952
  • 578
  • 3
  • 10
1

This is my solution:

def make_list(number):
    names = []
    for _ in range(number):
        names.append(input("Enter your name with a capital letter: "))
        print(names)
    return(names)
        
number = int(input("How many names need to be entered? "))
names = make_list(number)
for name in names:
    if name.startswith('A'):
        print("Name", name, "starts with A")

Results:

How many names need to be entered?3
Enter your name with a capital letter.RUNE
['RUNE']
Enter your name with a capital letter.PETER
['RUNE', 'PETER']
Enter your name with a capital letter.ANDERS
['RUNE', 'PETER', 'ANDERS']
Name ANDERS starts with A
Cow
  • 2,543
  • 4
  • 13
  • 25
  • This also works, good answer, however I would suggest changing the return statement from the function from: `return(names)` to just `return names` – Liam May 08 '22 at 05:32
  • _Generally the convention would be to not use brackets on a return statement_ – Liam May 08 '22 at 05:38
1

In python you can not loop through an integer. Try to switch for item in number with for item in range(number) I think this will solve your problem.

Tkun
  • 69
  • 2
  • This is not the only problem, the other issue that @user23952 also addressed is that the `make_list` function is not returning `names` as well as the correction from `if name[1] =='A'` to `if name[0]=='A'`. I don't know why this has an upvote when it clearly only fixes one part of the problem and the correct answer has already been posted by @user23952 here: https://stackoverflow.com/questions/71062965/i-am-new-to-python-and-i-wonder-what-is-wrong-with-this-code/71063027#71063027 – Liam May 08 '22 at 05:22
  • I suggest you edit your answer to include this or maybe you can delete your answer as a more complete answer has already been posted – Liam May 08 '22 at 05:29
0

for item in number: looks like you are attemping to repeat the loop number times. In python you do that like this: for item in range(0, number):. item will range from 0 to number-1

Andy Brown
  • 11,766
  • 2
  • 42
  • 61
  • Andy Brown, Thanks! – Joe Underwood Feb 10 '22 at 09:59
  • While this does solve one part of the answer, a more complete answer has been posted here: https://stackoverflow.com/questions/71062965/i-am-new-to-python-and-i-wonder-what-is-wrong-with-this-code/71063027#71063027. I suggest you edit your answer to include this or delete it as a more complete answer has already been posted – Liam May 08 '22 at 05:31
0

actually number is an integer, the error is saying in line 3 you are trying to loop through an integer which is not possible

Wrong : for item in number What you may do instead : for item in range(number)

Here is some reading on the range function https://www.w3schools.com/python/ref_func_range.asp

  • While this does get rid of the error, it doesn't solve the 2 other problems with the return from the function and the `name[1]` to `name[0]`, I suggest you either edit your answer to include this, or just delete your answer as a more complete answer has already been posted here https://stackoverflow.com/questions/71062965/i-am-new-to-python-and-i-wonder-what-is-wrong-with-this-code/71063027#71063027 – Liam May 08 '22 at 05:26