I need to create a program that takes in the names of students as keys and their corresponding list of grades as the values of the dictionary. Both, the names and grades must be taken from the user. There must be 3 student grades and they all must be positive! This is where I am having my problem. When I run the code and enter TWO grades instead of THREE student grades, it prints a message: "Exactly three grades are required for each student!" This works fine. However, when I enter a negative value for grades right after: 90, -7, 80 -> it doesn't give the message to enter the student's grades as positive. Can you help?
The following combinations can be tried: Student name and grade: Emily 90, 87, 78 Rio 70, 45 (Error message) Rio 70, 45, -90 (Expected error message does not pop up!)*
empty_dict = {}
while True:
s_names = input ("Please enter a student name (Enter -1 when done): ")
if s_names == '-1':
break
else:
if s_names not in empty_dict.keys():
s_grade = input ("Please enter this student's three grades separated by a comma: ")
s_grade = s_grade.split(',') #creates a list
for j in s_grade:
j = int(j)
if (j<0):
print ("All the grades must be a positive number!")
s_grade = input ("Please re-enter this student's grades as positive numbers: ")
s_grade = s_grade.split (',') #creates a list
if (len(s_grade) != 3):
print ("Exactly *three* grades are required for each student!")
s_grade = input ("Please re-enter this student's three grades separated by a comma: ")
s_grade = s_grade.split (',') #creates a list
empty_dict [s_names] = s_grade
else:
print ("This name has already been added; Please enter a different name: ")
continue
print (empty_dict)
Aucun commentaire:
Enregistrer un commentaire