Jump to content

Implement a program that requests a list of student names from the user and prints those names that start with letters A through M.

 

My code:

nameInput = input("Enter a list of names: ").split() def name_order(l:list):    '''Prints names starting with letters A through M'''    for i in l:        for n in i:            if n[0] in 'abcdefghijklmABCDEFGHIJKLM':                print(i)    return name_order(nameInput)

 

My output:

Enter a list of names: Ellie Steve Sam Owen Gavin
Ellie
Ellie
Ellie
Ellie
Ellie
Steve
Steve
Sam
Sam
Owen
Gavin
Gavin
Gavin

 

The correct output:

>>> Enter list: ['Ellie', 'Steve', 'Sam', 'Owen', 'Gavin']

Ellie

Gavin

 

I'm wondering why my program keeps on repeating names multiple times, and why it doesn't skip any names that are not between A through M?

Link to comment
https://linustechtips.com/topic/510920-python-list-problem/
Share on other sites

Link to post
Share on other sites

Implement a program that requests a list of student names from the user and prints those names that start with letters A through M.

 

My code:

nameInput = input("Enter a list of names: ").split() def name_order(l:list):    '''Prints names starting with letters A through M'''    for i in l:        for n in i:            if n[0] in 'abcdefghijklmABCDEFGHIJKLM':                print(i)    return name_order(nameInput)

 

My output:

Enter a list of names: Ellie Steve Sam Owen Gavin
Ellie
Ellie
Ellie
Ellie
Ellie
Steve
Steve
Sam
Sam
Owen
Gavin
Gavin
Gavin

 

The correct output:

>>> Enter list: ['Ellie', 'Steve', 'Sam', 'Owen', 'Gavin']

Ellie

Gavin

 

I'm wondering why my program keeps on repeating names multiple times, and why it doesn't skip any names that are not between A through M?

Follow your topics

 

You're looping through letters not words.

This ^

CPU: AMD FX-6300 4GHz @ 1.3 volts | CPU Cooler: Cooler Master Hyper 212 EVO | RAM: 8GB DDR3

Motherboard: Gigabyte 970A-DS3P | GPU: EVGA GTX 960 SSC | SSD: 250GB Samsung 850 EVO

HDD: 1TB WD Caviar Green | Case: Fractal Design Core 2500 | OS: Windows 10 Home

Link to comment
https://linustechtips.com/topic/510920-python-list-problem/#findComment-6811331
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×