Python range error. Im not sure how to make the program display "out of range if credits" entered are not in the range 0, 20, 40,60, 80, 100 and 120

I’m doing a python sort of grade calculator right now and i need to make the program display ‘Out of range’ if credits entered are not in the range 0, 20, 40,60, 80, 100 and 120.
Where it says incorrect it should say Out of range instead
This is the spec of the grade calc im trying to make
so far everything works apart from Part 1B where its supposed to display out of range
Second bullet point is the problem im struggling with

def Validation(StudentPassCredits, StudentDeferCredits, StudentFailCredits):
    if StudentPassCredits + StudentDeferCredits + StudentFailCredits == 120:

        if StudentPassCredits % 20 != 0 or StudentDeferCredits % 20 != 0 or StudentFailCredits % 20 != 0:
            print('Range error')
          
        else:
            Main_Version()

    else:
        print('Total incorrect')

I tried this but it wouldnt work and only displays Total incorrect instead of range error and i dont really know what to do now.

The rest of the program works as needed its just the out of range thats the issue

def Main_Version():
    if StudentPassCredits == 120:
        print('Your academic year progression outcome is :', 'Progress')

    elif StudentPassCredits == 100:
        print('Your academic year progression outcome is :', 'Progress (module trailer)')

    elif StudentFailCredits >= 80:
        print('Your academic year progression outcome is :', 'Exclude')

    else:
        print('Your academic year progression outcome is :', 'Do not progress – module retriever')

def Validation(StudentPassCredits, StudentDeferCredits, StudentFailCredits):
    if StudentPassCredits + StudentDeferCredits + StudentFailCredits == 120:

        if StudentPassCredits % 20 != 0 or StudentDeferCredits % 20 != 0 or StudentFailCredits % 20 != 0:
            print('Range error')
          
        else:
            Main_Version()

    else:
        print('Total incorrect')
try:
    StudentPassCredits = int(input('Please enter your current pass credits: '))
    StudentDeferCredits = int(input('Please enter your current defer credits: '))
    StudentFailCredits = int(input('Please enter your current fail credits: '))

    Validation(StudentPassCredits, StudentDeferCredits, StudentFailCredits) 

except ValueError:
    print('Integer required')

>Solution :

You can validate each credits.

def _validate_credits_in_range(credits: int, container: "Container[int]" = range(0, 121, 20)) -> bool:
    return credits in container

def Validation(StudentPassCredits: int, StudentDeferCredits: int, StudentFailCredits: int) -> None:
    if all(_validate_credits_in_range(credits) for credits in(StudentPassCredits, StudentDeferCredits, StudentFailCredits)):
        if StudentPassCredits + StudentDeferCredits + StudentFailCredits == 120:
            print('Total incorrect')
        else:
            Main_Version()
    else:
        print('Range error')

Leave a Reply