How To Create A Table in Python - python

I've posted this before, but I'm reposting it with more detailed information.
This is my assignment:
And, so far, this is my code:
# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
total = score1 + score2 + score3 + score4 + score5
average = total % 5
print(average)
# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
if score < 60:
print 'F'
elif score => 60 or score <= 69:
print 'D'
elif score => 70 or score <= 79:
print 'C'
elif score => 80 or score <= 89:
print 'B'
elif score => 90 or score <= 100:
print 'A'
# Define a function that prompts the user to input names and test scores
def input_data():
score1 = input('Enter score 1:')
name1 = input('Enter name 1:')
score2 = input('Enter score 2:')
name2 = input('Enter name 2:')
score3 = input('Enter score 3:')
name3 = input('Enter name 3:')
score4 = input('Enter score 4:')
name4 = input('Enter name 4:')
score5 = input('Enter score 5:')
name5 = input('Enter name 5:')
# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
As I said in the first post, I don't have a real problem with anything except setting up that table. I'm really confused!
I asked my instructor (this is an online course, BTW), and he said: "Its just using the print operator, like you have done in previous modules, your printing text , variables and the return value of a function."
Now I'm just wondering where to start.
EDIT: I've updated my code this:
# Define a function that prompts the user to input names and test scores
def input_data():
score1 = input('Enter score 1:')
name1 = input('Enter name 1:')
score2 = input('Enter score 2:')
name2 = input('Enter name 2:')
score3 = input('Enter score 3:')
name3 = input('Enter name 3:')
score4 = input('Enter score 4:')
name4 = input('Enter name 4:')
score5 = input('Enter score 5:')
name5 = input('Enter name 5:')
# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
if score < 60:
print ('F')
elif 60 <= score <= 69:
print ('D')
elif 70 <= score <= 79:
print ('C')
elif 80 <= score <= 89:
print ('B')
elif 90 <= score <= 100:
print ('A')
# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
total = score1 + score2 + score3 + score4 + score5
average = total / 5
print(average)
# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
for x in range(10):
print("{:<10}".format("{:0.1f}".format(x)), end='')
print ("Name\t\t\tnumeric grade\t\tletter grade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s") % ('name1', 'score1', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name2', 'score2', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name3', 'score3', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name4', 'score4', determine_grade)
print ("%s:\t\t\t%f\t\t%s") % ('name5', 'score5', determine_grade)
print ("---------------------------------------------------------------")
But when I run it:
EDIT #2: This is my code currently:
# Define a function that prompts the user to input names and test scores
def input_data():
score1 = input('Enter score 1:')
name1 = input('Enter name 1:')
score2 = input('Enter score 2:')
name2 = input('Enter name 2:')
score3 = input('Enter score 3:')
name3 = input('Enter name 3:')
score4 = input('Enter score 4:')
name4 = input('Enter name 4:')
score5 = input('Enter score 5:')
name5 = input('Enter name 5:')
# Define a function that caculates average test scores
def calculate_average(score1, score2, score3, score4, score5):
total = score1 + score2 + score3 + score4 + score5
average = total / 5
print(average)
# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
if score < 60:
print ('F')
elif 60 <= score <= 69:
print ('D')
elif 70 <= score <= 79:
print ('C')
elif 80 <= score <= 89:
print ('B')
elif 90 <= score <= 100:
print ('A')
# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
print ("Name\t\t\tnumeric grade\t\tlettergrade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % ('name1', 93, 'A'))
print ("%s:\t\t\t%f\t\t%s" % ('name2', 89, 'B'))
print ("%s:\t\t\t%f\t\t%s" % ('name3', 76, 'C'))
print ("%s:\t\t\t%f\t\t%s" % ('name4', 58, 'F'))
print ("%s:\t\t\t%f\t\t%s" % ('name5', 98, 'A'))
print ("---------------------------------------------------------------")
print (calculate_average)
And this is what happens when I run it:
Now, I have mainly two problems:
1) How do I get the input statements to execute and enter the data BEFORE the table is displayed?
2) How do I convert the numbers displayed so that they are in the '.2f' format? (I've already tried a few ways and none of them worked).
HOPEFULLY THE FINAL EDIT: I'm getting really close to the solution, but need help with a few more things.
Here is my code:
# Define a function that prompts the user to input names and test scores
score = input('Enter score 1:')
name1 = input('Enter name 1:')
score = input('Enter score 2:')
name2 = input('Enter name 2:')
score = input('Enter score 3:')
name3 = input('Enter name 3:')
score = input('Enter score 4:')
name4 = input('Enter name 4:')
score = input('Enter score 5:')
name5 = input('Enter name 5:')
# Define a function that determines a letter grade
# Based on the given score
def determine_grade(score):
if score < 60:
print ('F')
elif 60 <= score <= 69:
print ('D')
elif 70 <= score <= 79:
print ('C')
elif 80 <= score <= 89:
print ('B')
elif 90 <= score <= 100:
print ('A')
determine_grade(score)
# Define a function that caculates average test scores
def calculate_average(score):
total = score + score + score + score + score
average = total / 5
print(average)
calculate_average(score)
# Define a function that displays a letter grade for each score
# followed by the student's name and the average test score
def display_menu():
print ("Name\t\t\tnumeric grade\t\tlettergrade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % ('name1', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name2', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name3', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name4', 'score', determine_grade('score')))
print ("%s:\t\t\t%f\t\t%s" % ('name5', 'score', determine_grade('score')))
print ("---------------------------------------------------------------")
calculate_average(score)
And this is what happens when I press F5:
GUYS, I'M ALMOST DONE JUST NEED HELP WITH FORMATTING:
I created another file so I could reorganize my code a bit, so that's why there's no comments. This is what I have:
score1 = float(input('Enter score 1:'))
name1 = input('Enter name 1:')
score2 = float(input('Enter score 2:'))
name2 = input('Enter name 2:')
score3 = float(input('Enter score 3:'))
name3 = input('Enter name 3:')
score4 = float(input('Enter score 4:'))
name4 = input('Enter name 4:')
score5 = float(input('Enter score 5:'))
name5 = input('Enter name 5:')
def determine_letter_grade1(score1):
if score1 < 60.0:
print ('F')
elif 60.0 <= score1 <= 69.0:
print ('D')
elif 70.0 <= score1 <= 79.0:
print ('C')
elif 80.0 <= score1 <= 89.0:
print ('B')
elif 90.0 <= score1 <= 100.0:
print ('A')
def determine_letter_grade2(score2):
if score2 < 60.0:
print ('F')
elif 60.0 <= score2 <= 69.0:
print ('D')
elif 70.0 <= score2 <= 79.0:
print ('C')
elif 80.0 <= score2 <= 89.0:
print ('B')
elif 90.0 <= score2 <= 100.0:
print ('A')
def determine_letter_grade3(score3):
if score3 < 60.0:
print ('F')
elif 60.0 <= score3 <= 69.0:
print ('D')
elif 70.0 <= score3 <= 79.0:
print ('C')
elif 80.0 <= score3 <= 89.0:
print ('B')
elif 90.0 <= score3 <= 100.0:
print ('A')
def determine_letter_grade4(score4):
if score4 < 60.0:
print ('F')
elif 60.0 <= score4 <= 69.0:
print ('D')
elif 70.0 <= score4 <= 79.0:
print ('C')
elif 80.0 <= score4 <= 89.0:
print ('B')
elif 90.0 <= score4 <= 100.0:
print ('A')
def determine_letter_grade5(score5):
if score5 < 60.0:
print ('F')
elif 60.0 <= score5 <= 69.0:
print ('D')
elif 70.0 <= score5 <= 79.0:
print ('C')
elif 80.0 <= score5 <= 89.0:
print ('B')
elif 90.0 <= score5 <= 100.0:
print ('A')
average = (score1 + score2 + score3 + score4 + score5) / 5.0
def determine_letter_grade_avg(average):
if average < 60.0:
print ('F')
elif 60.0 <= average <= 69.0:
print ('D')
elif 70.0 <= average <= 79.0:
print ('C')
elif 80.0 <= average <= 89.0:
print ('B')
elif 90.0 <= average <= 100.0:
print ('A')
def display_menu():
for x in range(10):
print("{:<10}".format("{:0.1f}".format(x)), end='')
print ("Name\t\t\tnumeric grade\t\tletter grade")
print ("---------------------------------------------------------------")
print ("%s:\t\t\t%f\t\t%s" % (name1, score1, determine_letter_grade1(score1)))
print ("%s:\t\t\t%f\t\t%s" % (name2, score2, determine_letter_grade2(score2)))
print ("%s:\t\t\t%f\t\t%s" % (name3, score3, determine_letter_grade3(score3)))
print ("%s:\t\t\t%f\t\t%s" % (name4, score4, determine_letter_grade4(score4)))
print ("%s:\t\t\t%f\t\t%s" % (name5, score5, determine_letter_grade5(score5)))
print ("---------------------------------------------------------------")
print ('Average Score:', average, determine_letter_grade_avg(average))
And when I run it:

You should be looking towards str.format() and end='' for your print statement
Here's a little example
for x in range(10):
print("{:<10}".format("{:0.1f}".format(x)), end='')
The first part for the rounding to 1 decimal place for your float values is
"{:0.1f}".format(x)
The 0 is for no left padding, the following .1f is for rounding to 1 decimal place. Next is the main part
"{:<10}".format(...)
This will print with a minimum string length of 10 characters. So if you have a string such as Hello it is printed as Hello_____ ( _ to represent empty spaces) The 10 can be changed to a value of your choosing.
Finally the end=''. By default the end parameter is \n which is what creates the new line after each print, but by changing it you can form lines from multiple print statements. Just print normally or set end='\n' when you want to end the line.
Little something to note as well. When checking if a value is between two integers you can use this instead of checking it twice (also you would need and and operator instead of or )
elif 60 <= score <= 69:

You could do something like this:
def display_menu():
print "Name\t\t\tnumeric grade\t\tlettergrade"
print "---------------------------------------------------------------"
print "%s:\t\t\t%f\t\t%s" % ('name1', 50, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name2', 50, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name3', 23, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name4', 44, 'F')
print "%s:\t\t\t%f\t\t%s" % ('name5', 48, 'F')
print "---------------------------------------------------------------"
display_menu()
I used \t to give tabs in between.
This is the screenshot of the PythonShell Output

Well, I just got the solution back, and I did a real butchering on the code. But, I will post this so no one will have to suffer like I did:
# main function
def main():
# Local variables only have to define floats
average = 0.0
score1 = 0.0
score2 = 0.0
score3 = 0.0
score4 = 0.0
score5 = 0.0
# Get scores
score1 = float(input('Enter score 1:'))
name1 = input('Enter name 1:')
score2 = float(input('Enter score 2:'))
name2 = input('Enter name 2:')
score3 = float(input('Enter score 3:'))
name3 = input('Enter name 3:')
score4 = float(input('Enter score 4:'))
name4 = input('Enter name 4:')
score5 = float(input('Enter score 5:'))
name5 = input('Enter name 5:')
# Calculate average grade
average = calculate_average(score1, score2, score3, score4, score5)
#Display grade and average information in tabular form
print('Name\t\tnumeric grade\tletter grade')
print('----------------------------------------------------')
print(name1 + ':\t\t', score1, '\t\t', determine_grade(score1))
print(name2 + ':\t\t', score2, '\t\t', determine_grade(score2))
print(name3 + ':\t\t', score3, '\t\t', determine_grade(score3))
print(name4 + ':\t\t', score4, '\t\t', determine_grade(score4))
print(name5 + ':\t\t', score5, '\t\t', determine_grade(score5))
print('----------------------------------------------------')
print ('Average score:\t', average, '\t\t', \
determine_grade(average))
# The calc_average function returns average of 5 grades
def calculate_average(s1, s2, s3, s4, s5):
return (s1 + s2 + s3 + s4 + s5) / 5.0
# The determine_grade function receives a numeric
# grade and returns the corresponding letter grade
def determine_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'F'
# Call the main function.
main()

Related

Menu driven program

I'm trying to complete my menu driven program; however, I can't get this code to function properly. the goal is to get information from my previous code then get the evaluated output here. It keeps asking to execute my previous function before the menu pops up. get_list is the previous function that gets the list of numbers from the user.
previous code
def get_list(score_list=None):
if score_list is None:
score_list = []
n = int(input('How many scores do you want to enter? '))
for n in range(1, n + 1):
score = float(input('Enter score #{}: '.format(n)))
while True:
if 0 > score or score > 100:
print('Invalid score entered!!\n', 'Score should be a value between 0 and 100')
score = float(input('Enter score #{}: '.format(n)))
continue
else:
score_list.append(score)
break
return score_list
pass
problem code
def evaluate_list():
score_list = get_list()
lowest_score = min(score_list)
score_list.remove(lowest_score)
score_average = sum(score_list) / len(score_list)
if 90 < score_average <= 100:
grade = 'A'
elif 70 < score_average <= 80:
grade = 'B'
elif 40 < score_average <= 60:
grade = 'C'
elif score_average > 20 and score_average >= 40:
grade = 'D'
else:
grade = 'F'
print('----------Results--------------')
print('Lowest Score : ', lowest_score)
print('Modified List: ', score_list)
print('score Average: ', f'{score_average:.2f}')
print('Grade : ', grade)
evaluate_list()

Need advice regarding function assignment

**Thank you for all of the help in calculating the average letter grade that corresponds to average of scores. I also need to list each grade and the corresponding letter grade. I am a little confused on how to do this. Do I make another function to print this?
def main():
student_name = input('Please enter your first and last name: ')
scores = askForScore()
avg_score = calc_average(scores)
letter_grade = determine_grade(avg_score)
print(student_name)
print('The average of 8 tests is', letter_grade)
print('letter_grade\tnumber_grade')
print('--------------------------')
def askForScore():
score1 = float(input('Please enter the first test score:'))
score2 = float(input('Please enter the second test score:'))
score3 = float(input('Please enter the third test score:'))
score4 = float(input('Please enter the fourth test score:'))
score5 = float(input('Please enter the fifth test score:'))
score6 = float(input('Please enter the sixth test score:'))
score7 = float(input('Please enter the seventh test score:'))
score8 = float(input('Please enter the eigth test score:'))
return (score1, score2, score3, score4, score5, score6, score7, score8)
def calc_average(scores):
avg_score = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4] + scores[5] + scores[6] + scores[7]) / 8
return avg_score
def determine_grade(avg_score):
if avg_score >= 90 and avg_score <= 100:
return 'A'
elif avg_score >= 80 and avg_score <= 89:
return 'B'
elif avg_score >= 70 and avg_score <= 79:
return 'C'
elif avg_score >= 60 and avg_score <= 69:
return 'D'
else:
return 'F'
main()
Here you go. There were a few problems.
1) When you return a value from a function, you need to assign it to a variable so you can pass it into the next function.
2) String literals like the letter grade "F" need to be inside single or double quote marks.
def main():
student_name = input('Please enter your first and last name: ')
scores = askForScore()
avg_score = calc_average(scores)
letter_grade = determine_grade(avg_score)
print(letter_grade)
def askForScore():
score1 = float(input('Please enter the first test score:'))
score2 = float(input('Please enter the second test score:'))
score3 = float(input('Please enter the third test score:'))
score4 = float(input('Please enter the fourth test score:'))
score5 = float(input('Please enter the fifth test score:'))
return (score1, score2, score3, score4, score5)
def calc_average(scores):
avg_score = (scores[0] + scores[1] + scores[2] + scores[3] + scores[4]) / 5
return avg_score
def determine_grade(avg_score):
if avg_score >= 90 and avg_score <= 100:
return 'A'
elif avg_score >= 80 and avg_score <= 89:
return 'B'
elif avg_score >= 70 and avg_score <= 79:
return 'C'
elif avg_score >= 60 and avg_score <= 69:
return 'D'
else:
return 'F'
main()
Make sure you understand some Python concepts such as the scope of variables, return statements and function arguments. In your case, for instance, score1 ... score5 inside askForScore are not "readable" by calc_average. In fact, calc_average returns the values you need and those values need to be passed to the next function as arguments like this:
...
score1, score2, score3, score4, score5 = askForScore()
calc_average(score1, score2, score3, score4, score5)
...

Why isn't my try except catching if you put a string into the argument of score?

#Write a program to prompt for a score between 0.0 and 1.0.
#If the score is out of range print an error.
#If the score is between 0.0 and 1.0, print a grade using the following table:
#Score Grade >=0.9 A >=0.8 B >=0.7 C >=0.6 D <0.6 F
def computegrade(score):
try:
score1 = float(score)
if score1 <= 1.0 and score1 >= 0.9:
grade = "A"
elif score1 < 0.9 and score1 >= 0.8:
grade = "B"
elif score1 < 0.8 and score1 >= 0.7:
grade = "C"
elif score1 < 0.7 and score1 >= 0.6:
grade = "D"
elif score1 < 0.6 and score1 > 0:
grade = "F"
else:
grade = "ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!"
print(grade)
except:
print("ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!")
Everything seems to work as expected whether I enter an integer outside the range of 0.0 and 1.0 or enter within that range. I created a try except to catch if the user inputs a string for score. But it doesn't seem to catch it and I can't figure out why.
I tried this and it worked fine. Make sure the input is a string that can't be converted to float and you should be fine.
def computegrade(score):
try:
score1 = float(score)
if score1 <= 1.0 and score1 >= 0.9:
grade = "A"
elif score1 < 0.9 and score1 >= 0.8:
grade = "B"
elif score1 < 0.8 and score1 >= 0.7:
grade = "C"
elif score1 < 0.7 and score1 >= 0.6:
grade = "D"
elif score1 < 0.6 and score1 > 0:
grade = "F"
else:
grade = "ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!"
print(grade)
except:
print("ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!")
computegrade("fire")
Output:
ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!

grade input program python if else elif

I am trying to solve this grade input program. I need to input the grades between 0.6 to 1.0 and assign them a letter value. I can only use the if else method as we haven't gotten to the other methods yet in class...
score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
print "Your grade is an F"
elif score >= 0.6:
print "grade d"
elif score >= 0.7:
print "grade c"
elif score >= 0.8:
print "grade b"
elif score >= 0.9:
print "grade a"
else:
print "wrong score"`
You don't have to go highest to lowest score, you can also do this:
score = float(raw_input("Enter Score Grade:"))
if score < 0.60:
print "Your grade is an F"
elif score < 0.7:
print "grade d"
elif score < 0.8:
print "grade c"
elif score < 0.9:
print "grade b"
elif score <= 1.0:
print "grade a"
else:
print "wrong score"
If you do decide to check from highest to lowest, being consistent is a good practice. You can check your failing grade last:
score = float(raw_input("Enter Score Grade:"))
if score > 1:
print "wrong score"
elif score >= 0.9:
print "grade a"
elif score >= 0.8:
print "grade b"
elif score >= 0.7:
print "grade c"
elif score >= 0.6:
print "grade d"
else:
print "Your grade is an F"
As a reusable function :
def grade(score):
if score > 1:
return "wrong score"
if score >= 0.9:
return "grade a"
if score >= 0.8:
return "grade b"
if score >= 0.7:
return "grade c"
if score >= 0.6:
return "grade d"
return "Your grade is an F"
score = float(raw_input("Enter Score Grade:"))
print grade(score)
You should start from the largest grade first:
as you see 0.92 > 0.6 and also 0.92 > 0.9
But according to yout logic, it will satisfy the first if and will never reach last if.
Do something like this:
score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
print ("Your grade is an F")
elif score >= 0.9:
print ("grade a")
elif score >= 0.8:
print ("grade b")
elif score >= 0.7:
print ("grade c")
elif score >= 0.6:
print ("grade d")
else:
print ("wrong score")
You need to go from high grade to low grade. You need to change sco = int(float(score)) to score = float(score). It is not required to change it to int as you are comparing float
score = raw_input('Enter the Score')
score = float(score)
if score >= 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade
You have to take not of the first condition. If the first condition is equal to 0.9, it will equate to false and give the grade of 'B'. Also, it will be wise to convert the input to int first
score = int(raw_input('Enter the Score'))
score = float(score)
if score > 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade
It is therefore advisable to use the greater than or equal to so as to handle the situation when it is equal to the upper value(ie 0.9).
score = int(raw_input('Enter the Score'))
score = float(score)
if score >= 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade

Not understanding my python dilemma

I am trying to finish up an assignment for a class "grade determination"
Write a program that will input 3 test scores.The program should determine and display their average.The program should then display the appropriate letter grade based on the average.The letter grade should be determined using a standard 10-point scale: (A = 90-100; B = 80-89.999; C = 70-79.999, etc.)
So far what I've been able to put together, (and it works averaging)
def main():
score1 = input("What is the first test score? ")
score2 = input("What is the second test score? ")
score3 = input("What is the third test score? ")
scoreaverage = score1 + score2 + score3
score = (scoreaverage / 3)
if score < 60:
grade="F"
elif score < 70:
grade="C"
elif score < 80:
grade="B"
elif score < 90:
grade="A"
else:
print score, " is the student's grade average!"
main()
If I replace score with grade I get an error.
raceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 16, in main
UnboundLocalError: local variable 'grade' referenced before assignment
So my question is how do I get the letter grade to print correctly?
def main():
score1 = input("What is the first test score? ")
score2 = input("What is the second test score? ")
score3 = input("What is the third test score? ")
scoreaverage = score1 + score2 + score3
score = (scoreaverage / 3)
if score < 60:
grade="F"
elif score < 70:
grade="C"
elif score < 80:
grade="B"
elif score < 90:
grade="A"
else:
print score, " is the student's grade average!"
main()
This would be your indented code. Are you sure you get the error you are mentioning?
The following code would generate the error you are mentioning.
print grade, " is the student's grade average!"
You can fix it this way:
def main():
score1 = input("What is the first test score? ")
score2 = input("What is the second test score? ")
score3 = input("What is the third test score? ")
scoreaverage = score1 + score2 + score3
score = (scoreaverage / 3)
grade = 'Unknown'
if score < 60:
grade="F"
elif score < 70:
grade="C"
elif score < 80:
grade="B"
elif score < 90:
grade="A"
print "%s is the student's grade average!" % grade
main()
Welcome to SO. As explained by others, your problem was accessing grade before assigning a value to it. Here is another suggestion for doing what you want to do: (see the comments)
#disregard next line if you are using Python 3.3
from __future__ import division
#use a list to store the scores
scores = []
#if you are using 2.7 replace input with raw_input
#use float() to convert the input to float
scores.append(float(input("First score? ")))
scores.append(float(input("Second score? ")))
scores.append(float(input("Third score? ")))
#More general way of calculating the average
score = sum(scores) / len(scores)
#You could use a dictionary for score-grade mapping
grades = {100: 'A', 90: 'A', 80: 'B',
70: 'C', 60: 'D', 50: 'E',
40: 'F', 30: 'F', 20: 'F',
10: 'F', 0: 'F'}
#removes the remainder from the score
#to make use of the above dictionary
grade = grades[score - (score % 10)]
#Use string formatting when printing
print('grade: %s, score: %.2f' % (grade, score))
Your grade comparisons are off by 10 points. You won't assign anything to grade if score is between 90 and 100. And a piece of advice for a newbie programmer: Use a debugger. You'd have discovered the problem in just a few minutes had you stepped through it.
Try:
if score < 60:
grade="F"
elif score < 70:
grade="D"
elif score < 80:
grade="C"
elif score < 90:
grade="B"
else:
grade="A"

Categories