Python 3- assigns grades [duplicate] - python

This question already has answers here:
Python 3- Assign grade
(2 answers)
Closed 6 years ago.
• Define a function to prompt the user to enter valid scores until they enter a sentinel value -999. Have this function both create and return a list of these scores. Do not store -999 in the list!
• Have main( ) then pass this the list to a second function which traverses the list of scores printing them along with their appropriate grade.
I am having trouble with the getGrade function, it gives the error for i in grades: name 'grades' is not defined.
def main():
grade = getScore()
print(getGrade(grade))
def getScore():
grades = []
score = int(input("Enter grades (-999 ends): "))
while score != -999:
grades.append(score)
score = int(input("Enter grades (-999 ends): "))
return grades
def getGrade(score):
best = 100
for i in grades:
if score >= best - 10:
print(" is an A")
elif score >= best - 20:
print(score, " is a B")
elif score >= best - 30:
print(score, " is a C")
elif score >= best - 40:
print(score, " is a D")
else:
print(score, "is a F")
main()

You defined your function to be getScore() but you're calling getScores(), so as would be expected this throws an error because the function you're calling doesn't actually exist / hasn't been defined.
Addendum: Since you changed your question, after fixing the previous error.
Likewise you're calling grades, but grades is defined in your other function not within the scope of the function where you're trying to iterate over grades.

Related

Stuck on this Python program

Image to problem
The for loop works, though I can't figure out how to put the grade on a single line as shown in the photo. It asks for grades individualy when ran instead.
Here is my code:
num_students = int(input("Enter the number of students: "))
scores = []
for i in range(num_students):
scores.append(int(input("Enter a score: ")))
best_score = max(scores)
for i in range(num_students):
if scores[i] >= best_score - 10:
grade = "A"
elif scores[i] >= best_score - 20:
grade = "B"
elif scores[i] >= best_score - 30:
grade = "C"
elif scores[i] >= best_score - 40:
grade = "D"
else:
grade = "F"
print("Student", i, "score is", scores[i], "and grade is", grade)
Use a single input() call instead of a loop, and use .split() to get each individual score.
num_students = int(input("Enter the number of students: "))
scores = input(f"Enter {num_students} scores: ")
scores = [int(score) for score in scores.split()]
As John Gordon answered, your main issue is calling input inside a loop. That dictates that each user response will be on its own line. To fix this, call input once, then break the resulting string up using .split(). Split when called with no arguments will break a string into a list at each space.
Here is an example of how to do this (without the f strings and list comprehensions from John's answer):
num_students = int(input("Enter the number of students: "))
score_string = input("Enter " + str(num_students) + " scores: ")
scores = []
for score in score_string.split():
scores.append(int(score))

What to do when main() function isn’t calling/executing the other functions?

I created a main() function and also 3 others and I am trying to get the other 3 to be executed into main()
I had it working then I messed it up at the very end, so I commented it out since it is not calling the 3 other functions.
# The program displays the letter grade and associated message for each exam score
# and the average exam score
# The program will not contain any repeated code and have a minimum of two functions besides Main.
def main():
# ask user to enter test scores
score = int(input("Enter your test scores separate by spaces, no commas: "))
# display test score
def showLetters(num, letterGrade):
print(f"{num:.1f} is an {letterGrade}\n")
# display score message
def examScores(num):
if 90 <= num <= 100:
letterGrade = "A"
print("Excellent Work")
elif 80 <= num <= 89:
letterGrade = "B"
print("Nice job")
elif 70 <= num <= 79:
letterGrade = "C"
print("Not bad")
elif 60 <= num <= 69:
letterGrade = "D"
print("Room for improvement")
else:
letterGrade = "F"
print("Go back and review")
return letterGrade
# calculate average of all scores entered by user
def calcAverages(grades):
numbers = scores.split()
for i in range(len(numbers)):
numbers[i] = int(numbers[i])
print("Average exam score: ", sum(numbers) / len(numbers))
# ask user to repeat or quit
answer = str(input('Please enter "y" to run program again or "n" to exit: '))
if answer.lower() == 'n':
print('Thank you for using! Goodbye!')
sys.exit()
elif answer.lower() == 'y':
main()
# ??? = main()
# for n in ????? :
# showLetters(n, examScores(n, calcAverages(????)))
I made minor edits to your program and you now have the average values.
I have highlighted only the lines that need addition.
counter = 0 # tracks the number of times the function is called
#added the below 2 lines after counter
sumCredit = 0 #tracks the sum credit for all the students
sumStudyHrs = 0 #track the sum of the study hours for all the students
def myfunction():
global counter # tracks the number of times the function is called
#added two more global variables to keep track of the averages
global sumCredit #track the sum credit
global sumStudyHrs #track the sum study hours
the rest of the code can stay as is. Immediately after you print the student details, you need to accumulate the values so you can calculate the average.
print("Study hours: " + str(sumHours))
#added the below 2 lines after your print statements
sumCredit += classes
sumStudyHrs += sumHours
# Ask user if they want to end or restart
Now you need to use this information for your calculation.
When you print the average, use the above variables to compute the average.
print("Total Students: " + str(counter)) # tracks the number of times the function is called
#modified your average credits and average study with correct numerator
print("Average Credits: " + str(sumCredit / counter)) #computer average credits
print("Average Study Hours: " + str(sumStudyHrs / counter)) #compute average study hours
print('Thank you for using! Goodbye!')
This should give you the desired results.
Let me know if you need the full code.

How to assign a string to an empty string variable

grade=""
name=[]
courseworkMark=[]
examMark=[]
file=open("Test Marks.csv","r")
for line in file:
LineFromFile=(line.strip())
pupilName,pupilCoursework,pupilExam=LineFromFile.split(",")
name.append(str(pupilName))
courseworkMark.append(int(pupilCoursework))
examMark.append(int(pupilExam))
file.close
#-----------------------------------------------------------------------------------------------------------------------------------------------------------
def calculatePercentage(courseworkMark, examMark):
Percentage = round(((courseworkMark[index]+examMark[index])/160)*100)
return Percentage
#-----------------------------------------------------------------------------------------------------------------------------------------------------------
def calculateGrade(Percentage):
if Percentage >= 70:
#print("Your grade is an A")
grade=="A"
elif Percentage >=60 and Percentage <=69:
#print("Your grade is a B")
grade=="B"
elif Percentage >=50 and Percentage <=59:
#print("Your grade is a C")
grade=="C"
elif Percentage >=45 and Percentage <=49:
#print("Your grade is a D")
grade=="D"
else:
#print("You have failed")
grade=="Fail"
#-----------------------------------------------------------------------------------------------------------------------------------------------------------
def displayInformation(name,Percentage):
print(name[index],"got",Percentage,"% in total which is a/an",grade)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------
for index in range (len(name)):
Percentage = calculatePercentage(courseworkMark, examMark)
Grade = calculateGrade(Percentage)
displayInformation(name, Percentage)
Hi everyone, I am currently trying to assign another string to an already existing empty string variable however when I run the code, the value for grade is still empty as it is when first defined, I hope someone is able to help, many thanks
I see three problems.
You compare grade with strings instead of assigning: "==" instead of "="
You assign value to variable Grade, not grade.
calculateGrade returns nothing.

Why is each grade inputted the same no matter what the program defines including the average?

The program is as follows: Write a program that asks the user to input 5 test scores. The program should display a letter grade for each score and the average test score.
I am having issues with my output showing me a letter grade of the first inputted grade for every grade including the average. My numeric average is also being outputted as a letter grade, but it gives the correct average letter for that rather than the correct numeric result for the grade. I am in a low level coding class, therefore the program must be written in this format.
Here is the code:
a=float(input("Enter score 1:"))
b=float(input("Enter score 2:"))
c=float(input("Enter score 3:"))
d=float(input("Enter score 4:"))
e=float(input("Enter score 5:"))
def determine_grade(a,b,c,d,e):
num=a
if(num<=100 and num>=90):
grade='A'
elif(num<=89 and num>=80):
grade='B'
elif(num<=79 and num>=70):
grade='C'
elif(num<=69 and num>=60):
grade='D'
else:
grade='F'
return grade
def calc_average(a,b,c,d,e):
mean=(a+b+c+d+e)//5
if mean<=100 and mean>=90:
avggrade='A'
elif(mean<=89 and mean>=80):
avggrade='B'
elif(mean<=79 and mean>=70):
avggrade='C'
elif(mean<=69 and mean>=60):
avggrade='D'
else:
avggrade='F'
return avggrade
grade=determine_grade(a,b,c,d,e)
avggrade=determine_grade(a,b,c,d,e)
mean=calc_average(a,b,c,d,e)
determine_grade(a,b,c,d,e)
calc_average(a,b,c,d,e)
print("Score Numeric Grade Letter Grade")
print("--------------------------------------------")
print("Score 1: ",a," ",grade)
print("Score 2: ",b," ",grade)
print("Score 3: ",c," ",grade)
print("Score 4: ",d," ",grade)
print("Score 5: ",e," ",grade)
print("--------------------------------------------")
print("Average Score: ",mean," ",avggrade)
Whenever I put in (a):
num=a,b,c,d,e
in place of what is shown in the code, I get a syntax error saying:
TypeError: '<=' not supported between instances of 'tuple' and 'int'
If I don't do what has been shown in (a), my output is this:
Enter score 1:90
Enter score 2:88
Enter score 3:76
Enter score 4:68
Enter score 5:40
Score Numeric Grade Letter Grade
--------------------------------------------
Score 1: 90.0 A
Score 2: 88.0 A
Score 3: 76.0 A
Score 4: 68.0 A
Score 5: 40.0 A
--------------------------------------------
Average Score: C A
Okay, so there's some things to talk about here. First, I suggest that you experiment a bit more with simpler functions to properly get used to them. When you define determine_grade(a,b,c,d,e) your function will expect to find a,b,c,d,e arguments inside the function, but if you read it again, you'll notice that you only mention a. This means when you call grade=determine_grade(a,b,c,d,e) you're only calculating a's grade, and that's why you have the same grade for everyone (if you type grade with your code you'll notice it will output 'A'.
Another way to write a function that does exactly what you're looking for is this:
def determine_grade(score):
num=score
if(num<=100 and num>=90):
grade='A'
elif(num<=89 and num>=80):
grade='B'
elif(num<=79 and num>=70):
grade='C'
elif(num<=69 and num>=60):
grade='D'
else:
grade='F'
return grade
You input a score (that can be a,b,c,d or e) and it calculates a grade. If you want to get everyone's grade, then you can do something like this:
grades=[]
for i in [a,b,c,d,e]:
grades.append(determine_grade(i))
This will bring a list with all grades.
Then, if you want the average grade, it's better to calculate the average score, so you can use your same function to get the grade:
mean_score=np.array([a,b,c]).mean()
mean_grade(determine_grade(mean_score)
Doing this you'll have all the information you need with way less lines of code (And you can make it even more efficient using list comprehensions, but that's a bit more advanced).
To answer your question, your functions should be rewritten to accept a single input and then called several times. Also, your program is a classic case of something that should be DRYed up (Don't Repeat Yourself). Instead of manually typing a = input(), b = input() why not consider just tossing all these inputs into a list (perhaps with some string formatting on your prompt like my code). Below is a program that can easily be adjusted to accept arbitrary amounts of inputs:
input_scores = [] # make our list
scores_input = 5 # tell the program we have 5 scores to record
for input_count in range(1, scores_input + 1):
input_scores.append(float(input("Enter score {}:".format(input_count))))
def determine_grade(percent): # single input
if 90 <= percent <= 100:
grade = 'A'
elif 80 <= percent <= 89:
grade = 'B'
elif 70 <= percent <= 79:
grade = 'C'
elif 60 <= percent <= 69:
grade = 'D'
else:
grade = 'F'
return grade # single output
def calc_average(local_score_list): # expects the list we made earlier (many inputs)
mean = sum(local_score_list) // len(local_score_list)
if 90 <= mean <= 100:
average_grade = 'A'
elif 80 <= mean <= 89:
average_grade = 'B'
elif 70 <= mean <= 79:
average_grade = 'C'
elif 60 <= mean <= 69:
average_grade = 'D'
else:
average_grade = 'F'
return mean, average_grade # have our function return both values, since it calculated them both anyways
compiled_scores = []
for test_score in input_scores:
letter_grade = determine_grade(test_score) # calling our single input/output function many times in a for loop
compiled_scores.append((test_score, letter_grade)) # creating a tuple so each letter and percent is stored together
mean_percent, mean_letter_grade = calc_average(input_scores) # decompile both values from our function
print("Score Numeric Grade Letter Grade")
print("--------------------------------------------")
# now we iterate through all the scores made and print them
for count, result_tuple in enumerate(compiled_scores, 1): # just print the scores in a loop
print("Score {}: ".format(count), result_tuple[0], " ", result_tuple[1])
print("--------------------------------------------")
print("Average Score: ", mean_percent, " ", mean_letter_grade)
You can see that we condensed our input() lines -> a single line, and condensed the print statements as well! Now our program can take, say, 30 scores and work just as well!!! (This is huge)!! And all that is required to change is the number 5 at the top of our script -> 30. No longer will we have to type print('this') or input('that')! Our script will automatically adjust because we made it able to scale with our list.
My test case (using same numbers from your post):
Enter score 1:90
Enter score 2:88
Enter score 3:76
Enter score 4:68
Enter score 5:40
Score Numeric Grade Letter Grade
--------------------------------------------
Score 1: 90.0 A
Score 2: 88.0 B
Score 3: 76.0 C
Score 4: 68.0 D
Score 5: 40.0 F
--------------------------------------------
Average Score: 72.4 C
Let me know if you need further clarification on something I posted!

How do you structure a recursive function?

I'm trying to create a recursive function that checks if a grade can be converted to a float, then checks to make sure the grade is between 0 and 100. My logic as to why this would work is:
Checks to see if the grade can be converted to a float.
If it can't be converted into a float, the user receives an error message, has to input a new grade, and gradeChecker checks again.
If it can be converted into a float, gradeChecker checks to see if it's between 0 and 100
If the grade between 0 and 100, the grade is then appended to the Assignments dictionary.
If the grade isn't between 0 and 100, the user gives a new grade, which is passed back in to the function, so repeat starting at step 1.
Can you tell me how this code doesn't follow that logic?
def gradeChecker(grade):
while True:
try:
grade = float(grade)
while (grade < 0) or (grade > 100):
print("Sorry, your grade must be between 0 and 100.")
grade = input("What grade did you receive on: %s? " % assignment)
gradeChecker(grade)
except:
print("Sorry, that's an invalid input. Please only use numbers and decimal points!")
grade = input("What grade did you receive on: %s? " % assignment)
else:
break
Assignments[assignment].append(grade)
Edit: Indentations fixed!
For reference, here is the entirety of the code I am using to pass grade into gradeChecker.
import collections #We import collections so we can use an ordered dictionary
Assignments = collections.OrderedDict() #We create an ordered dictionary called 'Assignments'
#We have to add each assignment to our dictionary individually because it's an ordered dictionary
#You can't convert a regular dictionary, which is unordered, to an ordered dictionary
#If you used a regular dictionary, when looping through the assignments they would be displayed at random
Assignments['Exam 1'] = [0.2] #We add the assignment and the weight to the dictionary
Assignments['Exam 2'] = [0.2] #We add the assignment and the weight to the dictionary
Assignments['Exam 3'] = [0.2] #We add the assignment and the weight to the dictionary
Assignments['Homework'] = [0.2] #We add the assignment and the weight to the dictionary
Assignments['LQR'] = [0.1] #We add the assignment and the weight to the dictionary
Assignments['Final'] = [0.1] #We add the assignment and the weight to the dictionary
#We have to define our grade checker before it is called so that it can be used to verify users inputs
def gradeChecker(grade): #Used to verify that a users grades are in-between 0 and 100
while True:
try:
grade = float(grade)
while (grade < 0) or (grade > 100):
print("Sorry, your grade must be between 0 and 100.")
grade = input("What grade did you receive on: %s? " % assignment)
gradeChecker(grade)
except:
print("Sorry, that's an invalid input. Please only use numbers and decimal points!")
grade = input("What grade did you receive on: %s? " % assignment)
else:
break
Assignments[assignment].append(grade) #We append the grade to the assignment to be used with it's weight for the final grade calculation
#INPUT
for assignment in Assignments.keys(): #We loop through every assignment in our Assignments dictionary
grade = input("What grade did you receive on: %s? " % assignment) #We ask the user to enter their grade
gradeChecker(grade) #We check to see if their grade is valid by passing their input through the gradeChecker function
Using both recursion and loop here is redundant. Moreover, loops should be preferred to recursion in 'normal' languages like Python (i.e., not Lisp) unless you're dealing with an inherently recursive data structure like a tree.
Given that you never stated why you need to use recursion in the first place, I suggest to use something like this:
def input_grade(assignment):
while True:
try:
grade = input("What grade did you receive on: %s? " % assignment)
grade = float(grade)
if grade < 0 or grade > 1000:
raise ValueError
return grade
except:
print("Sorry, that's an invalid input. Please only use numbers and decimal points!")
for assignment in Assignments:
Assignments[assignment].append(input_grade(assignment))
Note 1. I didn't test the code above, but it should give you the general idea of how to structure this function.
Note 2. You should avoid modifying Assignments in the function, that's an unwanted side effect. Better offload this to the main code.
Note 3. Technically, the try..except in my code is an example of exception-based logic, which isn't a great thing (however, this is way better than using recursion here).
def gradeChecker(grade):
try:
grade = float(grade)
while (grade < 0) or (grade > 100):
print("Sorry, your grade must be between 0 and 100.")
grade = input("What grade did you receive on: %s? " % assignment)
gradeChecker(grade)
except:
print("Sorry, that's an invalid input. Please only use numbers and decimal points!")
grade = input("What grade did you receive on: %s? " % assignment)
else:
Assignments[assignment].append(grade)
the thing with your code is that it's redundant; the while statement is there to do what you already have the recursive calls doing. imo, the while statement is better since recursion often uses up more memory but thats just an opinion.

Categories

Resources