Bigslimm21
Bigslimm21

Reputation: 15

Getting python to validate one at a time

I have this program working but when I validate it needs to validate after each number not at the end. How would i make it check after each number input and still keep the functions by them selves. When ever i have it return a number directly after i get all sorts of errors.

def main():
    num1, num2, num3, num4, num5 = getinput()
    num1, num2, num3, num4, num5 = verify_num(num1, num2, num3, num4, num5)
    average_score,score = calc_average(num1, num2, num3, num4, num5)
    average_score = determine_grade(score)
    calprint(num1, num2, num3, num4, num5, score, average_score)

def getinput():
    num1 = int(input('Please enter your first test score: '))
    num2 = int(input('Please enter your second test score: '))
    num3 = int(input('Please enter your third test score: '))
    num4 = int(input('Please enter your fourth test score: '))
    num5 = int(input('Please enter your fifth test score: '))
    return num1, num2, num3, num4, num5

def verify_num(num1, num2, num3, num4, num5):
    while num1 < 0 or num1 > 100:
        print ('Error--- The number musy be at least 0 and not more than 100.')
        num1 = int(input('Please enter your first test score: '))
    while num2 < 0 or num2 > 100:
        print ('Error--- The number musy be at least 0 and not more than 100.')
        num2 = int(input('Please enter your second test score: '))
    while num3 < 0 or num3 > 100:
        print ('Error--- The number musy be at least 0 and not more than 100.')
        num3 = int(input('Please enter your third test score: '))
    while num4 < 0 or num4 > 100:
        print ('Error--- The number musy be at least 0 and not more than 100.')
        num4 = int(input('Please enter your fourth test score: '))
    while num5 < 0 or num5 > 100:
        print ('Error--- The number musy be at least 0 and not more than 100.')
        num5 = int(input('Please enter your fifth test score: '))
    return num1, num2, num3, num4, num5

def calc_average(num1, num2, num3, num4, num5):
    score = (num1 + num2 + num3 + num4 + num5)
    average_score = score / 5.0
    return score, average_score

def determine_grade(score):
    if score > 90:return '4.0'
    elif score > 80:return '3.0'
    elif score > 70:return '2.0'
    elif score > 60:return '1.0'
    return '0.0'

def calprint (num1, num2, num3, num4, num5, score, average_score):
    print
    print ("Score #1   ", format (num1))
    print ("Score #2   ", format (num2))
    print ("Score #3   ", format (num3))
    print ("Score #4   ", format (num4))
    print ("Score #5   ", format (num5))
    print ()
    print ("Average score",format (score))
    print ("Average grade",format (average_score))


main()

Upvotes: 1

Views: 185

Answers (2)

user764357
user764357

Reputation:

This should do what you are intending. The biggest key difference is, as Scott suggests, to make a generic approach to collecting the responses.

In this solution, getinput() collects and validates a single response, in isolation from all of the rest of the collections. This is then called as many times as required by the main function, and the validated data item is put into the scores data structure for later processing.

The next key difference is that we've abstracted all of the processing details into calprint, and which then calls calc_average and determine_grade when necessary, rather than having to pass the original 7 parameters into the function.

def main():
    scores = []
    for i in range(5):
        x = getinput(i)
        scores.append(x)
    calprint(scores)

def getinput(i):
    ordinal = { 1:'first',
                2:'second',
                3:'third',
                4:'forth',
                5:'fifth'}
    o = ordinal.get(i+1,"next")
    x = int(input('Please enter your '+o+' test score: '))
    while not(0 < x < 100):
        print ('Error--- The number musy be at least 0 and not more than 100.')
        x = int(input('Please enter your '+o+' test score: '))
    return x

def calc_average(scores):
    return (sum(scores)+0.0)/len(scores)

def determine_grade(score):
    if score > 90:return '4.0'
    elif score > 80:return '3.0'
    elif score > 70:return '2.0'
    elif score > 60:return '1.0'
    return '0.0'

def calprint (scores):
    print
    for i,s in enumerate(scores):
        print ("Score #%d   %s"%(i+1,format (s)))
    print
    print ("Average score ",format (calc_average(scores)))
    print ("Average grade ",format (determine_grade(calc_average(scores))))


main()

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49803

Have one function that asks for & reads a number until a valid one is entered, which then returns that valid number. Call it for each of your 5 inputs. (You may want to pass this function which number it is getting.)

Upvotes: 1

Related Questions