In: Computer Science
Answer using Jupyter Python #Prompt the user to enter a passing grade and store the value from #the input() into a variable. #Refer to the variable in problem 2 to complete the following: #Use the if statement to evaluate the only the first element in the #grades list using index notation. #If the grade is passing, display PASSED, otherwise display FAILED. """Problem 1d""" #Use the for statement to iterate on each element in the #grades list (from problem 1a) and displays each value. #Specifically, for each grade in the grades list, if the grade is #passing (referring to problem 1b), then display PASSED, otherwise display FAILED. """Problem 2a""" #Create a function that will evaluate if each grade in a list is considered #passing or failing. [hint: use the for statement and if...else together] #Store the total number of passing and failing grades in their own respective variables. #The function should return a dictionary with two key-value pairs where the keys are # the words PASSED and FAILED mapped to their total counts variable. """Problem 2b""" #Call the function and pass in the grades list (from problem 1a) as an argument.
As per your requirement, here is the Python code:
Rawcode:
#asking user to enter passing grade and storing it in variable
passing = int(input("Enter a passing grade: "))
#problem 1a
#creating grades list
grades = [20, 50, 43, 56, 67, 23, 96, 41, 85, 73, 43, 25, 43]
#Using the if statement to evaluate the only the first element in the #grades list using index notation.
if(grades[0] is passing):
print("PASSED")
else:
print("FAILED")
#problem 1b
#Using the for statement to iterate on each element in the #grades list (from problem 1a) and displays each value.
for i in grades:
print(i)
# if the grade is #passing (referring to problem 1b), then display PASSED
if(i is passing):
print("PASSED")
#otherwise display FAILED.
else:
print("FAILED")
#Creating a function that will evaluate if each grade in a list is considered #passing or failing
def gradeCount(grades):
#creating an empty dictionary
countDict = dict()
#initializing passed count
passCount = 0
#initializing failed count
failCount = 0
#looping through grades list
for i in grades:
#checking if they are passing
if(i is passing):
#then incrementing pass count
passCount = passCount + 1
#if it they are failed
else:
#then incrementing fail count
failCount = failCount + 1
#mapping passCount value to key PASSED
countDict['PASSED'] = passCount
#mapping failCount value to key FAILED
countDict['FAILED'] = failCount
#returns dictionary of count of both passed and failed
return countDict
#Calling the function and pass in the grades list (from problem 1a) as an argument.
print(gradeCount(grades))
Here is the screenshots of source code:
Here is the screenshot of output:
Hope this helps you. Please feel free to ask if you still have any queries.
Do upvote. Thank you !