0

can someone tell me what is wrong with my code?!!


def make_list(number):
      names =[]
      for item in range(0,number):
          names.append(input("Enter your name"))
      print(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)
4
  • Please don't change OP's original code @Sujay. Ask them what they intended in the comments. Commented Jul 9, 2021 at 3:41
  • Function make_list should return names. Otherwise, names = make_list(number) will set names to None. Commented Jul 9, 2021 at 3:42
  • The bottom part of the question was inside the function, where the function was called, and the inside loop ran in the number parameter which was provided inside the function. So I moved it out.@Vishnudev Commented Jul 9, 2021 at 3:44
  • What if the OP has it inside the function? @Sujay. Just saying, ask before changing. Commented Jul 9, 2021 at 3:45

5 Answers 5

1

In make_list, you're printing names, then implicitly returning None. Change the print to a return:

def make_list(number):
    names =[]
    for item in range(0,number):
        names.append(input("Enter your name with a capital letter."))
    return names

This way the caller will assign the list to names rather than None.

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

Comments

0

Rather than printing the name in the function, the fix is to return the names array.

def make_list(number):
      names =[]
      for item in range(0,number):
          names.append(input("Enter your name with a capital letter. "))
      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")

Sample Output:

How many names need to be entered? 2
Enter your name with a capital letter. Andrew
Enter your name with a capital letter. Abby
Name Andrew starts with A
Name Abby starts with A

Comments

0

you need to return the value from the function make_list to names in the main code

def make_list(number):
  names =[]
  for item in range(0,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")

Comments

0
def make_list(number):
    names =[]
    for item in range(0,number):
        names.append(input("Enter your name with a capital letter. "))
    return (names)
   
number = int(input("How many names need to be entered? "))
names = make_list(number)
for name in names:
    print(f"Name {name} starts with {name[0]}")

Comments

0

In your original code, number=int(input("blah")) ... was inside the function make_list() so make_list() never got called. Additionally, you need to return names at the end of make_list(), otherwise it implicitly returns None, the return value of print().

Comments

Your Answer

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