Как я могу сделать оценку, которая не соответствует параметрам, не считая среднего класса? - PullRequest
0 голосов
/ 29 сентября 2018
print('Hello, welcome to your grade calculator.')
GradeCount=0
totalGrades=0.0
moreStudent='y'

while moreStudent=='y' or moreStudent=='Y':
    grade=float(input('Enter a grade or a -1 to end: '))
    while grade !=-1:
        if grade>100 or grade<0:
            print('Invalid input. Please enter a value between 1 and 100.')
        elif grade>=90 and grade<=100:
            print('You got an A. Thats awesome.')
        elif grade>= 80 and grade<=89:
            print('You got a B. Good job.')
        elif grade>= 70 and grade<=79:
            print('You got a C. Thats fine I guess.')
        elif grade>=60 and grade<=69:
            print ('You got a D. Not very good.')
        elif grade<60:
            print ('You got an F. You fail.')
        totalGrades=totalGrades + grade
        GradeCount=GradeCount + 1
        grade=float(input('Enter the next grade or -1 to end: '))
    moreStudent=input('Are you a new student and ready to enter your grades? 
y or n: ')

print ('Class grade average:' , format(totalGrades/GradeCount, '.2f'))
print ('Number of grades entered:',GradeCount)

Как правило, наверху, где он проверяет входные данные, как я могу предотвратить включение любых недопустимых входных данных в среднее значение ниже?Кроме того, как я могу добавить текущий счет введенных оценок и промежуточный итог оценок?

1 Ответ

0 голосов
/ 29 сентября 2018

Вы можете просто поставить продолжение в конце вашего условия «Неверный ввод».Поскольку он находится в цикле while, вы также должны сбросить grade, скопировав / вставив ваш input вызов (grade=float(input('Enter the next grade or -1 to end: '))).

Пока вы это делаете, вы можете немного упростить свои условиянемного делая lower_limit <= grade < upper_limit.С изменениями это выглядит так (опять же, критической частью является continue и строка перед ней):

print('Hello, welcome to your grade calculator.')
GradeCount = 0
totalGrades = 0.0
moreStudent = 'y'

while moreStudent == 'y' or moreStudent == 'Y':
    grade = float(input('Enter a grade or a -1 to end: '))
    while grade != -1:
        if grade > 100 or grade < 0:
            print('Invalid input. Please enter a value between 1 and 100.')
            grade = float(input('Enter the next grade or -1 to end: '))
            continue
        elif 90 <= grade <= 100:
            print('You got an A. Thats awesome.')
        elif 80 <= grade < 90:
            print('You got a B. Good job.')
        elif 70 <= grade < 80:
            print('You got a C. Thats fine I guess.')
        elif 60 <= grade < 70:
            print ('You got a D. Not very good.')
        elif grade < 60:
            print ('You got an F. You fail.')
        totalGrades = totalGrades + grade
        GradeCount = GradeCount + 1
        grade = float(input('Enter the next grade or -1 to end: '))
    moreStudent = input('Are you a new student and ready to enter your grades? y or n: ')

print ('Class grade average:', format(totalGrades / GradeCount, '.2f'))
print ('Number of grades entered:', GradeCount)

Вы также можете решить эту проблему, вложив все свои elif вelse аналог if grade > 100 or grad < 0.

...