jeudi 25 mars 2021

not able to check if inputted value is in list(tried as checking string)

Im trying to check if the inputted value is a certain string, doing it directly as if var == 'str': didn't work so I tried this, where I check to see if its in a specified position of a list.

physics = list()
chemistry = list()
history = list()
geography = list()
comSci = list()
subjects = ['physics', 'chemistry', 'history', 'geography', 'comSci']
nameVar = None
subjectVar = None

for i in range(5):
    nameVar = input("enter the students name: ")
    subjectVar = input("enter the subject chosen: ")

    while subjectVar not in subjects:
        try:
            subjectVar = input("enter a valid subject: ")
        except:
            print("enter a valid subject!")

    if subjectVar.lower == subjects[0]:
        physics.append(nameVar)
    elif subjectVar.lower == subjects[1]:
        chemistry.append(nameVar)
    elif subjectVar.lower == subjects[2]:
        history.append(nameVar)
    elif subjectVar.lower == subjects[3]:
        geography.append(nameVar)
    elif subjectVar.lower == subjects[4]:
        comSci.append(nameVar)

print("physics:", len(physics), 'people')
print("chemistry:", len(chemistry), 'people')
print("history:", len(history), 'people')
print("geography:", len(geography), 'people')
print("comSci:", len(comSci), 'people')

please help

Aucun commentaire:

Enregistrer un commentaire