-2

This is my while loop so far. I ask the user how many assignments they completed, which can only be between 1 and 10 (and graded out of 0 to 5).

However, if they answer anything below 10, I want the to code to automatically enter it as zero, instead of the user having to manually enter 0 for each assignment they have not completed.

Right now my code will ask the user to input a grade 10 times, but I only want to input grades based on the users answer of how many assignments they completed. So if they answered 7, the question would be presented 7 times, and three 0 marks would be automatically factored in to the average.

while True:
   
    result = input("How many assignments did you complete? ")
   
  if result.isdigit() and 0 <= int(result) <= 10:
         break;
     print ("Try a number between 1 and 10") 
total = 0
gradeCount = 0
while gradeCount < 10:
    grade = float(input('What was assignment score: '))
    if grade < 0 or grade > 5:
        print('It should be a number from 0 to 5')
    else:
        gradeCount += 1
        total += grade
aaverage = total / 30 * 100
print('Average: ', aaverage)
a121
  • 798
  • 4
  • 9
  • 20
PCfuture
  • 1
  • 1

2 Answers2

0
while True:
   
    result = input("How many assignments did you complete? ")
    if result.isdigit() and 0 <= int(result) <= 10:
         break;
    print ("Try a number between 1 and 10") 
total = 0
gradeCount = 0
while gradeCount < int(result):
    grade = float(input('What was assignment score: '))
    if grade < 0 or grade > 5:
        print('It should be a number from 0 to 5')
    else:
        gradeCount += 1
        total += grade
aaverage = total / 50 * 100
print('Average: ', aaverage)

Just count the number of assignments. Also you have to divide by 50, because of the 10 max assignments, so 50 points

chess_lover_6
  • 632
  • 8
  • 22
0
while 1:
    result = input("How many assignments did you complete? ")
    if result.isdigit() and 0 <= int(result) <= 10:
         result = int(result)
         break
     print ("Try a number between 1 and 10") 
total = 0
for i in range(result):
    while 1:
        grade = float(input('What was assignment score: '))
        if 0 <= grade <= 5:
            break
        else:
            print('It should be a number from 0 to 5')
    total += grade
aaverage = total / 10
print('Average: ', aaverage)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thank you for taking your time to help me, your code executes exactly the way I want, but I am curious, is while 1 the same in principal as while True? is there a reason we would want it one way over the other? – PCfuture Mar 07 '21 at 05:58
  • I recommend reading: https://stackoverflow.com/questions/2764017/is-false-0-and-true-1-an-implementation-detail-or-is-it-guaranteed-by-the – chess_lover_6 Mar 07 '21 at 06:01