Reputation: 31
Output should be as follows:
Give a course grade (-1 exits): 5
Give a course grade (-1 exits): 3
Give a course grade (-1 exits): 1
Give a course grade (-1 exits): 0
Grades must be between 1 and 5 (-1 exits)
Give a course grade (-1 exits): 4
Give a course grade (-1 exits): -1
Average of course grades is: 3.2
for x in range(0,5):
for num in range(0,n+1,1):
sum = sum+num
average = sum / 5
sub1=int(input("Give a course grade (-1 exits): "))
sub2=int(input("Give a course grade (-1 exits): "))
sub3=int(input("Give a course grade (-1 exits): "))
sub4=int(input("Give a course grade (-1 exits): "))
sub5=int(input("Give a course grade (-1 exits): "))
if x>5:
print("Grades must be between 1 and 5 (-1 exits)")
elif x<1:
print("Average of course grades is: ", num)
The code doesnt work
Upvotes: 1
Views: 48
Reputation: 353
Your name error problem comes from the line
for num in range(0,n+1,1):
Because n is just undefined. But I don't think your are going through the good way with your script. If you want to keep 5 input values maximum, you can do as following:
my_inputs = []
for x in range(0, 5):
my_input = int(
input("Give a course grade (-1 exits): ")
)
if my_input == -1:
break
if my_input < 1 or my_input > 5:
print("Grades must be between 1 and 5 (-1 exits)")
continue
my_inputs.append(my_input)
if my_inputs:
average = sum(my_inputs) / len(my_inputs)
print(f'Avg is {average}')
Upvotes: 0
Reputation: 23815
Give the code below a try (feel free to ask questions)
values = []
while True:
value = int(input("Give a course grade (-1 exits): "))
if value == -1:
break
if value < 1 or value > 5:
print("Grades must be between 1 and 5 (-1 exits)")
continue
values.append(value)
if values:
print('Avg is {}'.format(sum(values) / len(values)))
Upvotes: 1