How do I implement a range function in this program?

Question:

I am making a program that allows students to predict their progression at the end of each academic year.

ble 1: Progression outcomes as defined by the University regulations.
Volume of Credit at Each Level
fist digit is Pass
second digit is Defer
third digit is Fail

i have already implmented this in to my program. however ive been asked to implement a range function in the the program so if anyone enters anything other then 0, 20, 40,60, 80,100 and 120, they should get an error that says “not in range” and asks them again to input the numbers again.

print(“Welcome to University of Westminster grade calculator”)

while True:
    passCR = input("Enter your pass credits")
    if passCR.isdigit():
        passCR = int(passCR)
        break
    else:
        print("Not an integer Value!try again")

while True:
    deferCR = input("Enter your defer credits")
    if deferCR.isdigit():
        deferCR = int(deferCR)
        break
    else:
        print("Not an integer Value!try again")

while True:
    failCR = input("Enter your fail credits")
    if failCR.isdigit():
        failCR = int(failCR)
        break
    else:
        print("Not an integer Value!try again")

def input_valid_number(which="pass"):
    while True:
        n = input("enter your {} credits: ".format(which))
        try:
            n = int(n)
            if 0 <= n <= 120 and (n % 20) == 0:
                return n
        except:
            pass
while True:
    passCR = input_valid_number("pass")
    deferCR = input_valid_number("defer")
    failCR = input_valid_number("fail")
    if sum([passCR, deferCR, failCR]) == 120:
        break
    print("Your Total Credits do not add up to 120. Please try again!")

if passCR == 120 and deferCR == 0 and failCR== 0:    #1
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("progress")   
elif passCR == 100 and deferCR == 20 and failCR== 0: #2
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("progress - module trailer")
elif passCR == 100 and deferCR == 0 and failCR== 20: #3
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("progress - module trailer")
elif passCR == 80 and deferCR == 40 and failCR== 0: #4
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 80 and deferCR == 20 and failCR== 20: #5
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 80 and deferCR == 0 and failCR== 40: #6
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 60 and deferCR == 60 and failCR== 0: #7
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 60 and deferCR == 40 and failCR== 20: #8
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 60 and deferCR == 20 and failCR== 40: #9
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 60 and deferCR == 0 and failCR== 60: #10
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 40 and deferCR == 80 and failCR== 0: #11
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 40 and deferCR == 60 and failCR== 20: #12
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 40 and deferCR == 40 and failCR== 40: #13
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 40 and deferCR == 20 and failCR== 60: #14
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 40 and deferCR == 0 and failCR== 80: #15
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Exclude")
elif passCR == 20 and deferCR == 100 and failCR== 0: #16
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 20 and deferCR == 80 and failCR== 20: #17
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 20 and deferCR == 60 and failCR== 40: #18
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 20 and deferCR == 40 and failCR== 60: #19
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 20 and deferCR == 20 and failCR== 80: #20
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Exclude")
elif passCR == 20 and deferCR == 0 and failCR== 100: #21
    print("Exclude",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 0 and deferCR == 120 and failCR== 0: #22
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 0 and deferCR == 100 and failCR== 20: #23
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 0 and deferCR == 80 and failCR== 40: #24
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 0 and deferCR == 60 and failCR== 60: #25
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Do not progress - module trailer")
elif passCR == 0 and deferCR == 40 and failCR== 80: #26
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Exclude")
elif passCR == 0 and deferCR == 20 and failCR== 100: #27
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Exclude")
elif passCR == 0 and deferCR == 0 and failCR== 120: #28
    print("Your pass, defer and fail credits are",passCR, deferCR, failCR)
    print ("Exclude")

1-how can i implement a range function that only allows the user to input 0,20,40,60,80,100,120?

2-the program should also let the user know if the total sum of pass,defer and fail is not 120, then they should get a message “total incorrect”. then rerun the same question.

PS i have included images of my program below for a clearer image.

https://imgur.com/0PzgE3J
https://imgur.com/sSURE4h

Asked By: Alireza1999

||

Answers:

For part 1, a simple func can test that the input value exists between the two end values and is a multiple of 20. The ‘mod’ func is good for the multiple part

def input_valid_number(which="pass"):
    while True:
        n = input("Enter your {} credits: ".format(which)).strip()
        if n.isdigit():
            n = int(n)
            if 0 <= n <= 120 and (n % 20) == 0:
                return n
            else:
                print("Invalid Selection. Please enter 0, 20, 40, 60, 80, 100, or 120!")
        else:
            print("Not an Integer Value. Please try again!")

For part 2, the below should do the trick…

while True:
    passCR = input_valid_number("pass")
    deferCR = input_valid_number("defer")
    failCR = input_valid_number("fail")
    if sum([passCR, deferCR, failCR]) == 120:
        break
    print("Your Total Credits do not add up to 120. Please try again!")
Answered By: GaryMBloom

Doing the same westminster uni task but, as late as I am, it would be more efficent to just check the number of credits for pass as no matter the other numbers the pass credits are the decider. For example all combinations with pass credit 100 have their outcome as ‘Progress module trailer’

Answered By: SharpRhyme
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.