1

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()
asked Nov 10, 2013 at 23:54

2 Answers 2

1

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.)

answered Nov 10, 2013 at 23:57
Sign up to request clarification or add additional context in comments.

3 Comments

But the getinput and verify_num need to be in their own functions. is it impossible to make it work the way i have it written? here is the directions maybe im reading them wrong. • Print grades for all individual tests and for the average test score. You must calculate grades in a function that has the test scores passed to it, one at a time. • Have an additional function that accepts the score. The score must be at least 0 and no more than 100. All scores must be validated in a separate function that has the scores passed to it, one at a time.
So break up the function I suggested into 2 parts: one to do the initial read, the other to validate that 1 value, and alternate calling them to get through all 5 input values.
Ok that's the trouble I'm having is keeping it how i have it and getting it to ask for input and validate in the same time.
0

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()
answered Nov 11, 2013 at 0:09

4 Comments

@Bigslimm21 That wasn't included in the question. You asked how to validate one at a time. Given that I'm not in your class, and it wasn't include, how was I to know?
Sorry I'm just having a hard time with the whole programing stuff its an online class so I'm teaching myself. I'm not grasping on how to keep the input and validate seperate but getting them to work at the same time.
How my code works now if the input is out of range it ask for the new correct input after you have done all five inputs it needs to ask as soon as you enter a number out of range.
@Bigslimm21 Thats exactly what this code does. I'd suggest reading through it, perhaps running it, altering it and generally trying to understand this relatively simple program.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.