Python Forum
Extracting list element with user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting list element with user input
#1
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?
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,276 Nov-12-2022, 03:47 AM
Last Post: deanhystad
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,870 May-14-2021, 07:14 AM
Last Post: perfringo
  Print user input into triangle djtjhokie 1 2,360 Nov-07-2020, 07:01 PM
Last Post: buran
  Extracting link list to json file naor 5 2,592 Sep-17-2020, 04:16 PM
Last Post: micseydel
  Changing Directory based on user input paulmerton4pope 13 7,958 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  How do you find the length of an element of a list? pav1983 13 4,835 Jun-13-2020, 12:06 AM
Last Post: pav1983
  how to add the user input from file into list wilson20 8 4,294 May-03-2020, 10:52 PM
Last Post: Larz60+
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,797 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,125 Nov-04-2019, 02:02 PM
Last Post: perfringo
  Extracting elements in a list to form a message using for loop Tony04 2 2,345 Oct-25-2019, 05:55 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020