How to get the right output from this python code? - python

I have a python code, it cannot get the correct output. the code is shown as follows:
score = int(input("Please input a score: "))
grade = ""
if score < 60:
grade = "failed"
elif score < 80: # between 60 and 80
grade = "pass"
elif score < 90:
grade = "good"
else:
grade = "excellent"
print("score is (0), level is (1)".format(score,grade))
Can anyone tell me where is the problem? Thank you so much!

You should change your if statement to:
if score <= 60:
grade = "failed"
elif score <= 80 and score > 60: # between 60 and 80
grade = "pass"
elif score <= 90 and score > 80:
grade = "good"
elif score > 90: #Assuming there is no max score since before, you left the last statement as an else.
grade = "excellent"
else: #Not necessary but always nice to include.
print("Not a valid score, please try again.")
And, as #loocid commented, change print("score is (0), level is (1)".format(score,grade)) to print("score is {0}, level is {1}".format(score,grade))
Though I prefer
print("score is " + str(score) + ", level is " + str(grade))

Related

python grade calculator to drop lowest score

im working on a python grade calculator for class where i can only use if/else/elif statements to drop the lowest score and then average the scores. this is what i have so far but i dont know how to determine the lowest score from the input using ONLY if/else statements. any help is appreciated!
# Input
sName = str(input("Name of person that we are calculating the grades for: "))
iTest1 = int(input("Test 1: "))
iTest2 = int(input("Test 2: "))
iTest3 = int(input("Test 3: "))
iTest4 = int(input("Test 4: "))
sDrop = str(input("Do you wish to drop the lowest grade Y or N? "))
# Test If Statements
if iTest1 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
if iTest2 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
if iTest3 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
if iTest4 <= 0:
print("Test scores must be greater than 0.")
raise SystemExit
# Drop Lowest If Statements
if sDrop != "Y" or "N" or "y" or "n":
print("Enter Y or N to drop the lowest grade.")
raise SystemExit
# Grade Scale
if score >= 97.0:
grade = "A+"
elif score >= 94.0 and score <= 96.9:
grade = "A"
elif score >= 90.0 and score <= 93.9:
grade = "A-"
elif score >= 87.0 and score <= 89.9:
grade = "B+"
elif score >= 84.0 and score <= 86.9:
grade = "B"
elif score >= 80.0 and score <= 83.9:
grade = "B-"
elif score >= 77.0 and score <= 79.9:
grade = "C+"
elif score >= 74.0 and score <= 76.9:
grade = "C"
elif score >= 70.0 and score <= 73.9:
grade = "C-"
elif score >= 67.0 and score <= 69.9:
grade = "D+"
elif score >= 64.0 and score <= 66.9:
grade = "D"
elif score >= 60.0 and score <= 63.9:
grade = "D-"
else:
grade = "F"
Test the first two to find the smallest of those. Now compare the other two in turn to smallest and update smallest if necessary.
if iTest1 < iTest2:
smallest = iTest1
else:
smallest = iTest2
if iTest3 < smallest:
smallest = iTest3
if iTest4 < smallest:
smallest = iTest4
Of course, all of this is a tedious way to write:
smallest = min([iTest1, iTest2, iTest3, iTest4])

can we create a quiz in python using only functions and also keep track of scores

I wanted to create a function which can keep track of the scores from each of the questions and then use it in another function which returns the final result to the user. I have no idea of how to do so.
# Function for question 1
def ask_question_one(difficulty):
score = 0
print("What is the capital city of Canada?")
answer = input()
if difficulty <= 3:
if answer == "Ottawa" or answer == "ottawa":
score = score + 10
print("Your score is:", score)
else:
print(score)
elif difficulty > 3 and difficulty <= 7:
if answer == "Ottawa" or answer == "ottawa":
score = score + 30
print ("your score is:", score)
else:
print("You have given an incorrect answer. You lose 5 points.")
score = score - 5
print("Your score is:", score)
else:
count = 1
while count < 3:
answer = input()
if answer != "Ottawa" or answer != "ottawa":
score = score - 5
print("Your score is:", score)
score = score + 50
print("Your score after two tries is:", score)
def display_result(difficulty, score):
if difficulty <= 3 and score > 30:
print("Your difficulty level is", str(difficulty) + " and your score is", str(score))
# Top-level code.
set_difficulty = int(input("Please enter a difficuty level between 1 and 10: "))
if set_difficulty == "":
print("You did not enter a difficulty level. Try again! Exiting quiz.")
exit()
elif set_difficulty == 0:
print("0(zero) is not a valid difficulty. Exiting quiz.")
exit()
elif set_difficulty > 10:
print("Select a difficulty level less than 10.")
exit()
else:
print("....Let's begin the quiz....")
ask_question_one(set_difficulty)
here is what i have done so far
Please explain the comment correctly. Then maybe I can answer (I am putting this here because I don't have enough reputation for posting comments :'( ).

How to make a grade calculator in Python

I am a beginner with python and I am working with this project that will print the result of the students.I am done with nearly everything execpt the percentage.See,in my code,the program will only print the percentage of the last person's marks.I need to make it so that it calculates percentage for everyone individually and then print it at the end.Your help will be much appreciated.Thanks
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
percentage =(marks / T_marks) * 100
elif ask == "print":
for (key,value) in data.items():
print(key,"::",value)
if percentage > 90:
print("Passed with A grade")
elif percentage >= 70 and percentage < 90:
print("Passed with B grade")
elif percentage >= 60 and percentage < 70:
print("Passed with C grade")
elif percentage >= 50 and percentage < 60:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
You need to compute percentage under the print case, this should get you what you want:
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
elif ask == "print":
for (key,value) in data.items():
# NOTE percentage is under the case when user asks for print
percentage =(value / T_marks) * 100
print(key,"::",value)
if percentage > 90:
print("Passed with A grade")
elif percentage >= 70 and percentage < 90:
print("Passed with B grade")
elif percentage >= 60 and percentage < 70:
print("Passed with C grade")
elif percentage >= 50 and percentage < 60:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
Also two hints: This code will output "You failed" if someone got a grade of 90. You need to set equality at 90 for one of the cases. Also python has simplified comparisons where and is not needed. Here is a simplified version, and corrected for case of 90 to get an A grade:
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
elif ask == "print":
for (key,value) in data.items():
percentage =(value / T_marks) * 100
print(key,"::",value)
if percentage >= 90:
print("Passed with A grade")
elif 90 > percentage >= 70:
print("Passed with B grade")
elif 70 > percentage >= 60:
print("Passed with C grade")
elif 60 > percentage >= 50:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
The input() method reads a string, but you cannot convert e. g. "4 4 4 5" to int. Method split() without arguments creates a list of the words in string as follow:
"4 5 5" -> ["4", "5", "5"]
Change your input to:
marks_string = input("Enter marks: ")
marks = [int(mark) for mark in marks_string.split()] # convertion to int
And change calculate of percentage:
percentage =(sum(marks) / T_marks) * 100
Indentation, fixed in edit:
T_marks = 1100
data = {}
while True:
ask = input("What do you want? ")
if ask == "y":
name = input("Enter your name: ")
marks = int(input("Enter marks: "))
data[name] = marks
percentage =(marks / T_marks) * 100
elif ask == "print":
for (key,value) in data.items():
print(key,"::",value)
if percentage > 90:
print("Passed with A grade")
elif percentage >= 70 and percentage < 90:
print("Passed with B grade")
elif percentage >= 60 and percentage < 70:
print("Passed with C grade")
elif percentage >= 50 and percentage < 60:
print("passed with D Grade")
else:
print("You failed")
else:
print("Your work has ended")
break
>>> What do you want? y
>>>Enter your name: Alex
>>>Enter marks: 12
>>>What do you want? y
>>>Enter your name: Michael
>>>Enter marks: 22
>>>What do you want? print
>>>Alex :: 12
>>>You failed
>>>Michael :: 22
>>>You failed
>>>What do you want?

Python program to calculate student grades [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
This is my first program in Python and I am having some trouble so forgive me if I have some simply syntax issues.
I am writing a program that calculates a student's final score based on one exam grade worth 60% and 7 other test scores worth a combined 40% of the final grade. The user is asked to input the one exam score then asked to input the 7 test scores which are read in a loop. The letter grade is then determined from the final score calculated from the exam and tests. After that a grade comment is printed corresponding to the letter grade given to the student. This is my code so far:
def read_test_scores() :
print("ENTER STUDENT ID: ")
id = int(input())
print("ENTER EXAM SCORE: ")
exam = int(input())
print("ENTER ALL TEST SCORES: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
score6 = int(input())
score7 = int(input())
sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
tavge = sum/7
return tavge
def compute_final_score(tavge, exam) :
final_score = 0.4 * tavge + 0.6 * exam
return final_score
def get_letter_grade(final_score) :
if 90 <= final_score <= 100:
grade = 'A'
elif 80 <= final_score <= 89:
grade = 'B'
elif 70 <= final_score <= 79:
grade = 'C'
elif 60 <= final_score <= 69:
grade = 'D'
else:
grade = 'F'
return grade
def print_comment(grade) :
if grade = 'A':
print "COMMENT: Very Good"
elif grade = 'B':
print "COMMENT: Good"
elif grade = 'C':
print "COMMENT: Satisfactory"
elif grade = 'D':
print "COMMENT: Need Improvement"
elif grade = 'F'
print "COMMENT: Poor"
read_test_scores()
print "TEST AVERAGE IS: " + str(tavge)
compute_final_score()
print "FINAL SCORE IS: " + str(final_score)
get_letter_grade(final_score)
print "LETTER GRADE IS: " + str(grade)
print_comment(grade)
Here's my answer. The code should run. Notes are inserted as comments.
# NOTE: I haven't checked whether your math is right, or
# if the computed values are correct. I did however get your
# script to work.
def read_test_scores():
print("ENTER STUDENT ID: ")
id = int(input())
print("ENTER EXAM SCORE: ")
exam = int(input())
print("ENTER ALL TEST SCORES: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
score6 = int(input())
score7 = int(input())
sum = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
tavge = sum / 7.0
# NOTE: if you want to use any variables from this function,
# then you have to "bring them outside" by "returning"
# them. Here, I return the values tavge, id, and exam. I noticed
# that bringing out "exam" is necessary since you'll
# be using it later on.
return tavge, id, exam
def compute_final_score(tavge, exam):
final_score = 0.4 * tavge + 0.6 * exam
return final_score
def get_letter_grade(final_score):
if 90 <= final_score <= 100:
grade = 'A'
elif 80 <= final_score <= 89:
grade = 'B'
elif 70 <= final_score <= 79:
grade = 'C'
elif 60 <= final_score <= 69:
grade = 'D'
else:
grade = 'F'
return grade
def print_comment(grade):
# NOTE `=` is for assignment. We use it when we want to
# tell python to make a variable mean something. For example:
# a = "some_name" basically means that when we call a, it would
# return the string "some_name".
# What you want to use here is `==` which is the equality operator.
# This checks whether or thing are equal.
if grade == 'A':
print("COMMENT: Very Good")
elif grade == 'B':
print("COMMENT: Good")
elif grade == 'C':
print("COMMENT: Satisfactory")
elif grade == 'D':
print("COMMENT: Need Improvement")
elif grade == 'F':
print("COMMENT: Poor")
# NOTE 1: you need to assign the function results to a
# variable (or variables), otherwise, the result or return value
# will go nowhere and you can't use it
tavge, id, exam = read_test_scores()
print "TEST AVERAGE IS: " + str(tavge)
# NOTE 2: variable names do not have to be the same as
# the name in their respective functions. Here, you can see
# that it will still run even if I changed the variable
# name final_score to my_variable. Although, of course, using
# final_score would still work.
# NOTE 3: the final_score function requires 2 inputs,
# namely tavge and exam. This basically means that you have to feed
# it with these 2 values for it to work. I took the
# tavge and exam variables as the results from your read_test_scores
# function
my_variable = compute_final_score(tavge, exam)
print("FINAL SCORE IS: " + str(my_variable))
grade = get_letter_grade(my_variable)
print("LETTER GRADE IS: " + str(grade))
print_comment(grade)
# FINAL NOTE: I haven't commented regarding coding style etc (like say
# for instance, there are best practices regarding variable names
# within functions, that is, if they should be similar to variable names
# outside the function), but regardless, the code is a good start. I
# would also advise you to try to narrow down your question first
# before posting. This can be done by running your code, and searching
# the internet for the particular erro messages, and if you're still stuck,
# ask here on stackoverflow.
there are many errors in your code, some of them are in the comment, but the most critical part is that you use global and local variables incorrectly
here is and example of fixing your code using the correct way to use global variables.
https://repl.it/repls/SorrowfulOddballSongbird
tavge = 0
exam = 0
sid = 0
final_score = 0
grade = ''
def read_test_scores() :
global sid
print("ENTER STUDENT ID: ")
sid = int(input())
global exam
print("ENTER EXAM SCORE: ")
exam = int(input())
print("ENTER ALL TEST SCORES: ")
score1 = int(input())
score2 = int(input())
score3 = int(input())
score4 = int(input())
score5 = int(input())
score6 = int(input())
score7 = int(input())
total = (score1 + score2 + score3 + score4 + score5 + score6 + score7)
global tavge
tavge = total/7
#return tavge
def compute_final_score() :
global final_score
final_score = 0.4 * tavge + 0.6 * exam
#return final_score
def get_letter_grade() :
global grade
if 90 <= final_score <= 100:
grade = 'A'
elif 80 <= final_score <= 89:
grade = 'B'
elif 70 <= final_score <= 79:
grade = 'C'
elif 60 <= final_score <= 69:
grade = 'D'
else:
grade = 'F'
#return grade
def print_comment() :
if grade == 'A':
print("COMMENT: Very Good")
elif grade == 'B':
print ("COMMENT: Good")
elif grade == 'C':
print ("COMMENT: Satisfactory")
elif grade == 'D':
print ("COMMENT: Need Improvement")
elif grade == 'F':
print ("COMMENT: Poor")
read_test_scores()
print ("TEST AVERAGE IS: " + str(tavge))
compute_final_score()
print ("FINAL SCORE IS: " + str(final_score))
get_letter_grade()
print ("LETTER GRADE IS: " + str(grade))
print_comment()
but you should consider using parameters instead using globals
As several people have mentioned you need to use == for comparison, you also are missing a colon after one of your if/else.
This is my take on your code. Keep in mind that this doesn't have and tests to make sure someone is actually entering in a number for a test score instead of text
"sum" is also the name of a built in function to Python, which sums up anything you provide it.
def read_test_scores():
scores = []
num_tests = 7
print("ENTER ALL TEST SCORES: ")
for i in range(num_tests):
score = input("Test " + str(i + 1) + ":")
scores.append(int(score))
return sum(scores) / num_tests
def compute_final_score(average, exam_score):
score = 0.4 * average + 0.6 * exam_score
return score
def get_letter_grade(finalized_score):
if 90 <= finalized_score <= 100:
letter_grade = 'A'
elif 80 <= finalized_score <= 89:
letter_grade = 'B'
elif 70 <= finalized_score <= 79:
letter_grade = 'C'
elif 60 <= finalized_score <= 69:
letter_grade = 'D'
else:
letter_grade = 'F'
return letter_grade
def print_comment(letter_grade):
if letter_grade == 'A':
print("COMMENT: Very Good")
elif letter_grade == 'B':
print("COMMENT: Good")
elif letter_grade == 'C':
print("COMMENT: Satisfactory")
elif letter_grade == 'D':
print("COMMENT: Need Improvement")
elif letter_grade == 'F':
print("COMMENT: Poor")
def get_student_id():
print("ENTER STUDENT ID: ")
identity = int(input())
return identity
def get_exam_score():
print("ENTER EXAM SCORE: ")
exam_score = int(input())
return exam_score
if __name__ == '__main__':
student_id = get_student_id()
exam = get_exam_score()
tavge = read_test_scores()
print("TEST AVERAGE IS: " + str(tavge))
final_score = compute_final_score(tavge, exam)
print("FINAL SCORE IS: " + str(final_score))
grade = get_letter_grade(final_score)
print("LETTER GRADE IS: " + str(grade))
print_comment(grade)

determining average grades and displaying letter grades

I'm working on a program for class that finds the average of 5 entered test scores then displays the letter grades relevant to each letter score. letter score is a 10 point system ( A = 90-100 B = 80-89, etc)
This is what I've put together so far but it doesn't seem to recognize "avg" in the syntax. any suggestions?
def main():
while true:
grade = int(input('Enter grade: '))
total += grade
avg = calc_average(total)
abc_grade = determine_grade(grade)
print('Average grade is: ' avg)
print('Letter grades for entered grades are: ' abc_grade)
def calc_average(total):
return total / 5
def determine_grade(grade):
if grade >= 90 and <= 100:
return 'A'
elif grade >= 80 and <= 89:
return 'B'
elif grade >= 70 and <= 79:
return 'C'
elif grade >= 60 and <= 69:
return 'D'
else:
return 'F'
main()
use:
print('Average grade is: '+str(avg))
print('Letter grades for entered grades are: '+abc_grade)
or
print('Average grade is: %.2f'%(avg))
print('Letter grades for entered grades are: %s'%(abc_grade))
_list = []
def calc_average(total):
return total / 5
def determine_grade(grade):
if grade >= 90 and grade <= 100:
return 'A'
elif grade >= 80 and grade <= 89:
return 'B'
elif grade >= 70 and grade <= 79:
return 'C'
elif grade >= 60 and grade <= 69:
return 'D'
else:
return 'F'
while True:
grade = int(input('Enter grade: '))
_list.append(grade)
avg = calc_average(sum(_list))
abc_grade = ' '.join([determine_grade(mark) for mark in _list])
if len(_list) > 5:
break
print('Average grade is: ', avg)
print('Letter grades for entered grades are: ', abc_grade)
def main():
print("This is a program which displays the grade from a score")
print("")
grade = eval(input("What is the value of the score : "))
print("")
if 90 <= grade <= 100:
print("Your get an A")
elif 80 <= grade <= 89:
print("Your get a B")
elif 70 <= grade <= 79:
print("Your get a C")
elif 60 <= grade <= 69:
print("Your get a D")
else:
print("Your get an F")
main()
This Worked for me.. A few minor changes except your code is working fine.
def main():
total = 0;avg = 0;abc_grade = 0
def calc_average(total):
return total / 5
def determine_grade(grade):
if 90 <= grade <= 100:
return 'A'
elif 80 <= grade <= 89:
return 'B'
elif 70 <= grade <= 79:
return 'C'
elif 60 <= grade <= 69:
return 'D'
else:
return 'F'
while(True):
grade = int(input('Enter grade: '))
total += grade
avg = calc_average(total)
abc_grade = determine_grade(grade)
print('Average grade is: ' + str(avg))
print('Letter grades for entered grades are: ' + str(abc_grade))
main()
Hope you can find out differences. :)

Categories

Resources