Reputation: 13
EDIT: Random "for loop" output fixed. Now am receiving "list index out of range error".
I am meant to be making a grading program that is based on randomly generated letter grades (A-F). The program asks for the amount of assignments and prints the assignment number with the letter grade. It should also find the average grade of the randomly generated grade. The output should look something like this:
Assignment 1 grade is an F
Assignment 2 grade is a C
Assignment 3 ...
The average is a ...
I have a list set up to randomly generate the letter grade. I am having trouble setting code to find the average of the letter grades. Also, my loop does not spit out a new letter grade every time as it should. Here is some of my code:
numberlist = []
numberlist.append("A")
numberlist.append("B")
numberlist.append("C")
numberlist.append("D")
numberlist.append("F")
count = 1
for i in range(total):
numberindex = random.randint(0, 5)
print("The grade for assignment", count, \
"is a {}".format(numberlist[numberindex]))
count = count + 1
It currently prints:
Assignment 1 grade is an F
Assignment 2 grade is an F
Assignment 3 grade is an F...
Upvotes: 1
Views: 1341
Reputation: 384
You only need to make a random choice every time the loop iterates to a new element.
numberlist = ["A", "B", "C", "D", "F"]
for i in range(total):
choice = random.choice(numberlist)
print('The grade for assignment {i} is a {mark}'.format(i=i+1, mark=choice))
Upvotes: 1
Reputation: 767
You should put the random number generation inside of the loop:
for i in range(total):
numberindex = random.randint(0, 5) ####here!
print("The grade for assignment", count, \
"is a {}".format(numberlist[numberindex]))
count = count + 1
Upvotes: 1