grade = raw_input("What was your score?")
if grade >= 93:
print "For a score of " + grade + ", your grade is an A"
elif grade >= 90 and grade < 93:
print "For a score of " + grade + ", your grade is an A-"
elif grade >= 87 and grade < 90:
print "For a score of " + grade + ", your grade is a B+"
elif grade >= 83 and grade < 87:
print "For a score of " + grade + ", your grade is a B"
elif grade >= 80 and grade < 83:
print "For a score of " + grade + ", your grade is a B-"
elif grade >= 77 and grade < 80:
print "For a score of " + grade + ", your grade is a C+"
elif grade >= 73 and grade < 77:
print "For a score of " + grade + ", your grade is a C"
elif grade >= 70 and grade < 73:
print "For a score of " + grade + ", your grade is a C-"
elif grade >= 67 and grade < 70:
print "For a score of " + grade + ", your grade is a D+"
elif grade >= 63 and grade < 67:
print "For a score of " + grade + ", your grade is a D"
elif grade >= 60 and grade < 63:
print "For a score of " + grade + ", your grade is a D-"
else:
print "For a score of " + grade + ", your grade is an F"
No matter what is entered as raw input, the statement printed is always "For a score of ___, your grade is an A" even if it should be a B or a D or anything else
raw_input returns a string, not a number. Before the comparison, you need to turn it into int or float.
Btw, if you are starting with python, go for 3 instead of 2. 2 is already EOL and support for libraries is starting to decrease.
You are comparing an integer and a string.
You need to cast the string to an integer using int().
So, you could for example do something like this
grade = int(raw_input("What was your score?"))
Of course, this would then leave you with the problem that that (ugly) concatenation you are doing to display your output message would stop working (as you cannot concatenate strings and integers).
Rather than directly using string concatenation, it's 99% of the times better to use format, like this:
print "For a score of {}, your grade is an A".format(grade)
grade = int(input("Enter your score"))
use python 3.
Related
I am creating a program that takes users and stores them in list and takes their marks and afterwards it grades them. I've done the user input part and the marks as well along with the average of them but I am struggling to give them grade and print it along with their name so it will be like Name marks Grade. If anyone can help me with this I'll be thankful greatly here is my code
students=[]
for i in range (2):
x=(input("Enter Student Name. \n"))
students.insert(i,x)
i+=1
print(students)
grades = []
for student in students:
grade = eval(input(f"Enter the grade for {student}: "))
grades.append(grade)
result = list(zip(students, grades))
print(result)
average = sum(grades) / len(grades)
print ( "Average is: " + str(average))
total = sum(grades)
# print ("Total is: " + str(total))
print("Highest marks", max(list(zip(grades, students))))
print("Lowest marks", min(list(zip(grades, students))))
## To do assign grades to each according to their marks
I am struggling to give them grade and print it along with their name so it will be like Name marks Grade.
Look at the below - it uses zip and loop
names = ['Jack','Dan']
grades = [67,86]
def _get_grade(grade):
if grade < 50:
return 'C'
elif grade >= 50 and grade <= 75:
return 'B'
else:
return 'A'
for name,grade in zip(names,grades):
print(f'Name: {name} - Grade: {_get_grade(grade)}')
So I need to make a program that grades 10 students individually, then displays the average grade for all 10 students.
I think this is how the grading should look, but im not sure how to set up a count on the number of times its graded, or how to set up the average function. Help would be most welcome. I am a horrible coder.
score = int(input("Enter a score between 0 and 100: "))
if score >=89:
print("The grade is an A")
elif score>=79:
print("The grade is a B")
elif score>=69:
print("The grade is a C")
elif score>=59:
print("The grade is a D")
else:
print("The grade is a F")
You can try using a counter variable that goes from 0-9 and use a while loop to check and increment that counter value and after each loop calculates the average and keeps doing this till the last value has been entered
Counter = 0
average = 0
while (Counter <= 10):
score = int(input("Enter a score between 0 and 100: "))
if score >= 89:
print("The grade is an A")
elif score >= 79:
print("The grade is a B")
elif score >= 69:
print("The grade is a C")
elif score >= 59:
print("The grade is a D")
else:
print("The grade is a F")
Counter = Counter + 1
average = (score + average)
average = average/Counter
print("Average of all 10 students:",average)
So right now the code is configured specifically for just 10 loops but you can give the user the power to determine this by suggesting a value to end the loop and the while loop will check for this value and when the loop sees the value it exits the loop and gives a value
Also with each loop there is a counter value that keeps increasing and an average value that holds the sum of the values entered and when the loop exits and average is calculated by dividing the sum by the counters and this gives the results
This should do the trick.
total = 0
for i in range(10):
score = int(input("Enter a score between 0 and 100: "))
if score >= 89:
print("The grade is an A")
elif score >= 79:
print("The grade is a B")
elif score >= 69:
print("The grade is a C")
elif score >= 59:
print("The grade is a D")
else:
print("The grade is a F")
total += score
print("Average of all 10 students:", total/10)
In the for loop you get the score of each student and then add it to the total of all students. When the loop ends you divide it with the total number of students so 10.
total score / total number of students total / 10
You can define a function that finds the average score. Store all the scores in a list and then pass the list as an argument in the function
def avg(scores): #assuming scores is the name of the list
avg = sum(scores)/len(scores)
return "Average score is {}".format(avg)
Or
return f'Average score is {avg}' #for python3
Not sure I understand what you mean by number of times it is graded but I think you can include a while loop and a variable that increases by 1 every time it grades.
I have been struggling with this code for a couple hours and am not seeing much in previous questions that relates to it. I've gone down the rabbit hole trying to make it work so I think it's pretty inefficient at this point as well as isn't outputting in the manner I'd like.
I am trying to get user input in the form of multiple grade entries, evaluate them based on grading criteria and then output the students grade in letter form
Any advice on getting it to output correctly or on any better coding techniques would be greatly appreciated. Thanks ahead of time from a novice coder.
Here is my code:
"""10.1 - write a program that reads a list of scores and then
assigns letter grades based on the criteria:
A if score is >= best - 10
B if score is >= best - 20
C if score is >= best - 30
D if score is >= best - 40
F otherwise
"""
grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
number_of_students = []
for i in range(len(grades_list_valid)):
number_of_students.append(i)
for grade in grades_list_valid: #create criteria to assign each letter grade
best_score = max(grades_list_valid) #get the highest grade
if grade >= best_score - 10:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is A")
elif grade >= best_score - 20:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is B")
elif grade >= best_score - 30:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is C")
elif grade >= best_score - 40:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is D")
else:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is F")
and here is the output:
Enter Students scores seperated by a space: 40 55 70 58
Student 3 score is 58 and grade is C
Student 3 score is 58 and grade is B
Student 3 score is 58 and grade is A
Student 3 score is 58 and grade is B
The desired output is:
Student 0 score is 40 and grade is C
Student 1 score is 55 and grade is B
Student 2 score is 70 and grade is A
Student 3 score is 58 and grade is B
Its because your first loop to get i is completing before you make use of i, then i is already 3 when you loop through to print the results. Instead you should get the index in your second loop like this:
grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
best_score = max(grades_list_valid)
for i, grade in enumerate(grades_list_valid):
letter_grade = "F"
if grade >= best_score - 10:
letter_grade = "A"
elif grade >= best_score - 20:
letter_grade = "B"
elif grade >= best_score - 30:
letter_grade = "C"
elif grade >= best_score - 40:
letter_grade = "D"
else:
letter_grade = "F"
print("Student {} score is {} and grade is {}".format(i, grade, letter_grade))
and then eliminate the first loop.
After looking again at the question i also noticed you were grabbing the "best score" from the same list every time, which would make each student have the same score. Im guessing you just want to grab the score for each student. I updated my answer to work for that - not sure if it was your desired behaviour.
Edit: looked at it again and realized what you were doing with best score, fixed my answer here's the results when i run it:
Enter Students scores seperated by a space: "70 80 93 77 31"
Student 0 score is 70 and grade is C
Student 1 score is 80 and grade is B
Student 2 score is 93 and grade is A
Student 3 score is 77 and grade is B
Student 4 score is 31 and grade is F
grades_input = input("Enter Students scores seperated by a space: ") #get user input of student's grades
grades_list = grades_input.split() #split user input into a list
grades_list_valid = [ int(x) for x in grades_list ] #convert items into integers
number_of_students = []
for i in range(len(grades_list_valid)):
number_of_students.append(i)
i=0
for grade in grades_list_valid: #create criteria to assign each letter grade
best_score = max(grades_list_valid) #get the highest grade
print(i)
if grade >= best_score - 10:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is A")
elif grade >= best_score - 20:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is B")
elif grade >= best_score - 30:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is C")
elif grade >= best_score - 40:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is D")
else:
print("Student {}".format(number_of_students[i]), "score is {}".format(grades_list_valid[i]), "and grade is F")
i+=1
this the correct way,in your code i=3 which is why you are getting the same answer for each student
As Quinn said, your top loop iterates over i, which finishes at i = 3. Future references to i will yield 3.
Some things I noticed:
1) What you can do is check their grade at the same time as you loop through.
2) Additionally, you don't need to find the maximum each loop, you can get it once outside the loop and then reference it inside.
3) Finally, it looks quite ugly when you have all of these print statements, it makes it much nicer to read if you assign the grade in the if statements and then print once with the found grade, like so:
best_score = max(grade_list_valid)
for i in range(len(grades_list_valid)):
curr_score = grades_list_valid[i]
if(curr_score >= best_score - 10):
grade = "A"
...
print("Student {} score is {} and grade is {}".format(i, curr_score, grade))
There are some further small optimisations you can make as well. For example, the list comprehension you have:
[ int(x) for x in grades_list ]
Is the classic case for a map:
map(int, grades_list)
I'm writing a python program that drops the lowest of four test scores. The program first prompts the user to enter their first four test scores, then the program should drop the lowest test score and take the average of the remaining three test scores. The program should then print the final letter grade. This is what I have so far and I keep receiving an error when dropping the lowest score.
#Enter four test scores as percentages (%)
test1 = int(input("Enter grade 1: 90"))
test2 = int(input("Enter grade 2: 80"))
test3 = int(input("Enter grade 2: 70)")
test4 = int(input("Enter grade 2: 80)")
#Drop lowest test score
print("The average, with the lowest score dropped" )
total =(test1 + test2 + test3)
#Calculate average
def calc_average(total):
return total /3
#Grade scale
def determine_score(grade):
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >=70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "F"
#Calculate final letter grade
print("The final grade is")
main()
I tried to write a program with the things that I understood from you.
Here is the explanation:
Taking four grades from user.
Dropping the lowest one.
Taking average.
Giving a letter grade according to the average.
Here is the code:
test1 = int(input("Enter grade 1: "))
test2 = int(input("Enter grade 2: "))
test3 = int(input("Enter grade 3: "))
test4 = int(input("Enter grade 4: "))
x = min(test1,test2,test3,test4)
total = float(test1 + test2 + test3 + test4 - x)
avg = total / 3
print("Your average is " + str(avg))
def determine_letter(grade):
letter =""
if grade >= 90:
letter = "A"
elif grade >= 80:
letter = "B"
elif grade >= 70:
letter = "C"
elif grade >= 60:
letter = "D"
else:
letter = "F"
print("The final letter grade is " + letter)
determine_letter(avg)
I'm making a change counter and I'm having trouble printing the percentage for a grade, whenever I run the program I can enter as many inputs as I want, however, when I type done, which is supposed to terminate the program and leave the user with the percentage and letter grade, it just ends the program. If I could get any advice it would be greatly appreciated.
here's my code:
grade=""
total=0
count=1
scores=''
while scores != 'done':
scores=input("Enter Homework score: ")
if scores.isdigit():
numeric=int(scores)
percentage=(numeric*10/count)
elif percentage >= 92 and percentage < 100:
letter = 'A'
elif percentage >= 87 and percentage < 92:
letter = 'B+'
elif percentage >= 80 and percentage < 87:
letter = 'B'
elif percentage >=77 and percentage < 80:
letter = 'C+'
elif percentage >=70 and percentage < 77:
letter = 'C'
elif percentage >= 67 and percentage < 70:
letter = 'D+'
elif percentage >= 60 and percentage < 67:
letter = 'D'
elif percentage < 60 and percentage >= 0:
letter= 'F'
elif (numeric) < 0:
print("Score must be between 0 and 10")
elif (numeric) > 10:
print("Score must be between 0 and 10")
elif (scores)== 'done':
print(percentage,"% and you got an, ",letter)
Your conditional logic is flawed. You are never assessing the grade (letter) if the score.isdigit():
while scores != 'done':
scores=input("Enter Homework score: ")
if scores.isdigit():
numeric=int(scores)
percentage=(numeric*10/count)
if percentage >= 92 and percentage < 100:
letter = 'A'
elif percentage >= 87 and percentage < 92:
letter = 'B+'
...
It is often cleaner to jump out of the loop if the initial condition is false, e.g.:
while scores != 'done':
scores=input("Enter Homework score: ")
if not scores.isdigit():
continue
numeric=int(scores)
percentage=(numeric*10/count)
if 92 <= percentage < 100:
letter = 'A'
elif 87 <= percentage < 92:
letter = 'B+'
...
Also in python your shouldn't be afraid of exceptions. A common idiom in python is EAFP (Easier to Ask for Forgiveness than Permission):
while scores != 'done':
scores=input("Enter Homework score: ")
try:
numeric = int(scores)
except ValueError:
continue
You might also want to think about better ways of doing the large grade if elif elif ... block. E.g. an alternative approach would be define a dictionary of the grades:
grades = {'A': (92, 100), 'B+': (87, 92)} # Etc..
score = 93
_, letter = max((low <= score < high, letter) for letter, (low, high) in grades.items())
print(letter) # 'A'
Your code should look similar to the one below. Although I still cannot assess the logic behind your program (because you did not explain that in your question, e.g. percentage=(numeric*10/count) does not seem quite right to me, etc.), but the code below solves your current problem (based on your current question).
grade=""
total=0
count=1
scores=''
percentage = 0
while scores != 'done':
scores=input("Enter Homework score: ")
if scores.isdigit():
numeric=int(scores)
if numeric < 0:
print("Score must be between 0 and 10")
elif numeric > 10:
print("Score must be between 0 and 10")
percentage=(numeric*10/count)
if percentage >= 92 and percentage < 100: # I would change this to if percentage >= 92 and percentage <= 100:
letter = 'A'
elif percentage >= 87 and percentage < 92:
letter = 'B+'
elif percentage >= 80 and percentage < 87:
letter = 'B'
elif percentage >=77 and percentage < 80:
letter = 'C+'
elif percentage >=70 and percentage < 77:
letter = 'C'
elif percentage >= 67 and percentage < 70:
letter = 'D+'
elif percentage >= 60 and percentage < 67:
letter = 'D'
elif percentage < 60 and percentage >= 0: #I would change this to else:
letter= 'F'
print(percentage,"% and you got an, ",letter)