mercredi 4 décembre 2019

Added an else statement inside a for loop, and now only the else part prints even when if is true

This is my first ever computer science course so forgive me if this answer is really obvious. I wrote the below code and it worked just fine. I just needed to add an else statement to account for an invalid name input.

Here is is before:

    mynames = ['Naomi', 'James', 'Amos', 'Alex', 'Bobbie', 'Josephus', 'Fred', 'Camina', 'Julie', 'Prax', 'Christien', 'Anderson', 'Havelock', 'Ashford', 'Bull', 'Anna', 'Arjun', 'Souther', 'Carissa', 'Samara']
    myscores = [89, 98, 76, 76, 84, 93, 82, 64, 63, 75, 76, 86, 96, 75, 86, 100, 99, 87, 84, 94]

    name = input("Please enter a name:")
    #search through mynames to find the name
    while name!='q':
        for x in range(20):
            if mynames[x] == name:
                 print(mynames[x], "scored", myscores[x])
        name = input("Please enter a name:")

And here it is after:

     mynames = ['Naomi', 'James', 'Amos', 'Alex', 'Bobbie', 'Josephus', 'Fred', 'Camina', 'Julie', 'Prax', 'Christien', 'Anderson', 'Havelock', 'Ashford', 'Bull', 'Anna', 'Arjun', 'Souther', 'Carissa', 'Samara']
    myscores = [89, 98, 76, 76, 84, 93, 82, 64, 63, 75, 76, 86, 96, 75, 86, 100, 99, 87, 84, 94]

    name = input("Please enter a name:")
    #search through mynames to find the name
    while name!='q':
        for x in range(20):
            if mynames[x] == name:
                print(mynames[x], "scored", myscores[x])
            else:
                print("That name is not in this class.")
                name = input("Please enter a name:")
        name = input("Please enter a name:")

It just keeps printing "That name is not in this class." no matter what I type in. SOS

Aucun commentaire:

Enregistrer un commentaire