#Week 5 Ch 6 Ex 7: #This program gets input for 5 tests scores, assigns grades, then uses a function to calculate the average and assign an overall grade. #Written by Greg Lamb #Last Modified on 04/27/2015 #Perform Function to assign letter grade def letterGrade(testScore): if (testScore in range(90, 101)): return "A" elif (testScore in range(80, 90)): return "B" elif (testScore in range(70, 80)): return "C" elif (testScore in range(60, 70)): return "D" else: return "F" #Declare loop variables testScoreTotal = 0 numTests = 5 #The loop iterates 5 times and stores the accumulated test scores for the 5 tests which are passed into the calcAverage function for test in range(1,numTests+1): testScore = float(input("Enter the numerical test score for test#{} ".format(test))) testScoreTotal = testScoreTotal + testScore print("is: {}. The letter grade is {} ".format(testScore,letterGrade(testScore))) #Get Average test score and grade def calcAverage(testScoreTotal): return testScoreTotal/5; #Result print("The overall average is: {}.".format(calcAverage(testScoreTotal))) print("The average letter grade is {}".format(letterGrade(calcAverage(testScoreTotal))))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question