I'm trying to get a nested loop to work correctly but I'm having a lot of trouble with it. I have a working code with lists but I want to add an else statement that declares a choice invalid and to try again and then exit and return to the first loop but I can't seem to get it to work without it repeating the invalid print code for every item in the list. Can anyone tell me what I'm doing wrong? What works:
myNames = ["Jim", "Sarah", "Jason", "Lynne", "Ginny", "Joe", "Susan"]
myScores = [88, 92, 95, 84, 85, 92, 89]
print ("Student names: Jim, Sarah, Jason, Lynne, Ginny, Joe, Susan")
name = input("Please enter a students name to find their score or q to quit: ")
while name != 'q':
for x in range(7):
if myNames[x] == name:
print(myNames[x])
print(myScores[x])
name = input("Please enter a students name to find their score or q to quit: ")
What doesn't work:
myNames = ["Jim", "Sarah", "Jason", "Lynne", "Ginny", "Joe", "Susan"]
myScores = [88, 92, 95, 84, 85, 92, 89]
print ("Student names: Jim, Sarah, Jason, Lynne, Ginny, Joe, Susan")
name = input("Please enter a students name to find their score or q to quit: ")
while name != 'q':
for x in range(7):
if myNames[x] == name:
print(myNames[x])
print(myScores[x])
else:
print("Invalid name please try again")
name = input("Please enter a students name to find their score or q to quit: ")
Aucun commentaire:
Enregistrer un commentaire