1

I have the program working how I'd like it to, but I cannot seem to figure out how to add validation for the user test score input. The inputs need to be from 0 - 100 and validate each entered score.

How would I modify my code to use a validation loop for input to be >= 0 and <= 100 in the prompt_scores function? I previously attempted a while loop but it was ignored when placed on each individual input.


def calc_average(scoreOne, scoreTwo, scoreThree):
    average = (scoreOne + scoreTwo + scoreThree)/3
    return average

def determine_grade(studentScore):
    if studentScore < 60:
        return "F"
    elif studentScore < 70:
        return "D"
    elif studentScore < 80:
        return "C"
    elif studentScore < 90:
        return "B"
    elif studentScore < 101:
        return "A"

def prompt_scores():
    enteredScoreOne = int(input("Please enter score 1: "))
    enteredScoreTwo = int(input("Please enter score 2: "))
    enteredScoreThree = int(input("Please enter score 3: "))

    return enteredScoreOne, enteredScoreTwo, enteredScoreThree

def print_results(scoreOne, scoreTwo, scoreThree):
    print("\nScore\tLetter Grade" )
    print(str(scoreOne) + "\t\t" + determine_grade(scoreOne), \
    str(scoreTwo) + "\t\t" + determine_grade(scoreTwo), \
    str(scoreThree) + "\t\t" + determine_grade(scoreThree), sep = "\n")

def main():
    scoreOne, scoreTwo, scoreThree = prompt_scores()
    print_results(scoreOne, scoreTwo, scoreThree)
    print("-----------------------")
    print("Average score: " + str(int(calc_average(scoreOne, scoreTwo,scoreThree)))) 
    print(" Final grade: " + determine_grade(int(calc_average(scoreOne, scoreTwo, scoreThree))))

    rerun_main = input("Do you want to continue? Enter y/n: ")

    if rerun_main == "Y" or rerun_main == "y":
        main()

main()
Scott
  • 11
  • 2
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Jul 23 '17 at 18:09

2 Answers2

2
enteredScoreOne = int(input("Please enter score 1: "))
while enteredScoreOne not in range(0, 101):
    print("[!] Invalid input!")
    enteredScoreOne = int(input("Please enter score 1: "))

And so on for the other variables.

If you're running Python 2 (given that you're using input to read strings, you're not, but I'll add this just in case), you'd better replace in range(...) with (0 <= enteredScoreOne <= 100) as range would return a list, which would consume a little bit of extra memory.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
0

You can check the entered value while getting input if you use function and can force the user to enter the value between 0-100 using recursion also i can see you are using additional functions for which python itself has a built in eg: sum(). Also try to save memory and processing wherever possible now it may not seem a big issue but when you have 1000 lines of code even these small things will save you. Here i mean instead of calling a function twice you can save the result in a variable and use it. I added all these in my code and have given the answer.

def get_input(): 
    try:
        score=int(input("please enter score : "))
        if (score >=0 and score <100):
            return score
        else:
            print("Score should be inbetween 0-100. Try again :-(:-(:-(")
            get_input()
     except:#if user enters any special char except float or int tell him to enter int
            print("Only integer is accepted")

def determine_grade(avg):
    if studentScore < 60:
        return "E"
    elif studentScore < 70:
        return "D"
    elif studentScore < 80:
        return "C"
    elif studentScore < 90:
        return "B"
    elif studentScore < 101:
        return "A"


def print_results(*args):
     for i,j in enumerate(args):
         print("score "+str(i)+" = "+str(j)+" grade = "+determine_grade(j))


def main():
    score1 = get_input()
    score2 = get_input()
    score3 = get_input()
    print_results(score1, score2, score3)
    print("-----------------------")
    avg=sum([score1,score2,score3])/3
    print("Average score: " + str(avg))
    print(" Final grade: " + determine_grade(avg))
    rerun_main = input("Do you want to continue? Enter y/n: ")
    if rerun_main.lower() == "y": 
        main()

main()
Mani
  • 5,401
  • 1
  • 30
  • 51