Python Forum

Full Version: Extracting list element with user input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am completely new to Python, and I need some help working with lists.

So here is a list:
Subjects=['SPLA100','SPLA107','SPLA219','INFO114','INFO128','INFO214']

I want to be able to let a user choose which classes to print out through input.
The user can choose to view the classes either by prefix(SPLA or INFO) or level(100 or 200) or both.

I've written this so far:
def choose:
    print('Choose class (Press enter for all)')
    prefix=input('Class: ')
    prefix=prefix.upper()
    level=input('Level: ')
    for subject in Subjects:
        print(emne)
I know I have to use a nested for loop somehow, but I don't know how to use the input information to extract just the subjects that the user is asking for.

Can anyone help?
Here's a hint to get you started
subjects=['SPLA100','SPLA107','SPLA219','INFO114','INFO128','INFO214']
prefix = 'SPLA'

for subject in subjects:
    if prefix in subject:
        print(subject)
Output:
SPLA100 SPLA107 SPLA219
From the above you can work out how to use prefix from user input.

The following gives clues to how you could check the number in a subject is between numbers.
a = 'SPLA107'.split('SPLA')
print(a)
a = a[1]
print(a)
a = int(a)
if 100 <= a <= 199:
    print(f'{a} is between 100 and 200')
Output:
['', '107'] 107 107 is between 100 and 200