grade input program python if else elif - python

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

Related

Letter for Grade for multiple students

def getGradeInfo():
numStudent = int(input('Total number of students: '))
getGrades = input(f'Enter {numStudent} scores: ')
gradeListNum = getGrades.split(' ')
maxScore = int(max(gradeListNum[0:numStudent]))
gradeListLetter = []
for grade in gradeListLetter:
if int(grade) >= maxScore - 10:
gradeListLetter.append('A')
elif int(grade) >= maxScore - 20:
gradeListLetter.append('B')
elif int(grade) >= maxScore - 30:
gradeListLetter.append('C')
elif int(grade) >= maxScore - 40:
gradeListLetter.append('D')
else:
gradeListLetter.append('F')
a = 1
while a<= numStudent:
print(f'Student{a} score is {gradeListNum[a-1]} and grade is')
a+=1
getGradeInfo()
I can get the input for the number of students in the code but can't figure out how to assign the letter to each of the student's grades for the print statement
As Adam Oppenheimer said,
for grade in gradeListLetter:
should be
for grade in gradeListNum:
Also,
print(f'Student{a} score is {gradeListNum[a-1]} and grade is')
should be
print(f'Student {a}: score is {gradeListNum[a - 1]} and grade is {gradeListLetter[a - 1]}')
You are iterating over gradeListLetter which is empty, try iterating over gradeListNum instead.
You also aren't printing the letter grade in your print statement.

Make a grading Calculator with Functions?

I am doing a school assignment, and can not figure out despite how much I have tried, how to make the raw input change except to make each one individually,
Below is my assignment parameters:
Prompt the user to enter in a numerical score for a Math Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for a English Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for a PE Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for a Science Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for an Art Class. The numerical score should be between 0 and 100.
Call the letter grade function 5 times (once for each class).
Output the numerical score and the letter grade for each class.
And this is what I have done:
class_list=(['Math','Science','English','P.E.','Art'])
for i in class_list:
grades = int(input('What is your score for math: ',class_list[1])),
def score (grade):
if grade>=93:
return ("A")
elif grade >=90 and grade<=93:
return ("-A")
elif grade >= 87 and grade<=90:
return("B+")
elif grade>=83 and grade >=87:
return ("B")
elif grade >= 80 and grade >=83:
return ("B-")
elif grade >= 77 and grade >80:
return ("C+")
elif grade >= 73 and grade >= 77:
return ("C")
elif grade >= 70 and grade >=73:
return ("C-")
elif grade>= 67 and grade >= 70:
return ("D+")
elif grade >=63 and grade >=67:
return ("D")
elif grade >=60 and grade >=63:
return ("D-")
else:
return ("F")
print ("For an average score of"),grade, ("your grade is %s") % (score (grade))
Any advice?
For Python 3.2+ (iirc)
# convert the things to Py 2.x :); The below is one of the way;
class_list=['Math','Science','English','P.E.','Art']
my_dict = {}
for (idx,subject) in enumerate(class_list):
my_dict[subject] = int(input(f"What is your score for {class_list[idx]} :")) # my_dict.update({subject : int(input(f"What is your score for {class_list[idx]} : "))})
# Calculate letter grade of each student
def assign_letter_grade(grade):
if grade >= 93:
return ("A")
elif grade >= 90 and grade <= 93:
return ("A-")
elif grade >= 87 and grade <= 90:
return("B+")
elif grade>=83 and grade >= 87:
return ("B")
elif grade >= 80 and grade >= 83:
return ("B-")
elif grade >= 77 and grade > 80:
return ("C+")
elif grade >= 73 and grade >= 77:
return ("C")
elif grade >= 70 and grade >=73:
return ("C-")
elif grade>= 67 and grade >= 70:
return ("D+")
elif grade >= 63 and grade >= 67:
return ("D")
elif grade >= 60 and grade >= 63:
return ("D-")
else:
return ("F")
for subject in my_dict:
print(f"For {subject}, a score of {my_dict[subject]}, you are eligible to get grade as {assign_letter_grade(my_dict[subject])}")
For Python 2.x
class_list=['Math','Science','English','P.E.','Art']
my_dict = {}
for idx,subject in enumerate(class_list):
print("What is your score for :", class_list[idx])
my_dict[subject] = int(raw_input())
print(my_dict)

python beginner syntax statement if-elif

I'm learning python, and I'm trying with a set of exercises, but I'm stuck on the next one:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A") elif score >= 0.8:
print ("B") elif score >= 0.7:
print ("C") elif score >= 0.6:
print ("D") elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
but I'm get the next message error:
IndentationError: unindent does not match any outer indentation level on line 7 elif score >= 0.8: ^ in main.py
The indentation (= number of tabs / spaces in front of each line) is important in python. The code you posted isn't indented properly. A correct indentation would look like this:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A")
elif score >= 0.8:
print ("B")
elif score >= 0.7:
print ("C")
elif score >= 0.6:
print ("D")
elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
The first line is always un-indented. When starting a block (e.g. try:, if:, elif:, ...), all following lines that belong within this block are indented with 4 spaces more than the opening line. "Closing" a block is done by writing the next statement with less indentation.
Another example:
if False:
print(1)
print(2)
# prints nothing because both print statements are part of the if condition
if False:
print(1)
print(2)
# prints 2 because the `print(2)` statement is not part of the if condition
Does this answer your question?
Your indentations should be like this:
inp_1 = input('your score between 0.0 and 1.0')
try:
score = float(inp_1)
if 0.9 <= score <= 1.0:
print ("A")
elif score >= 0.8:
print ("B")
elif score >= 0.7:
print ("C")
elif score >= 0.6:
print ("D")
elif score < 0.6:
print ("Your grade is an F")
else:
print ('your score is more than 1.0')
except:
print ('invalid input, please try with a number')
I think you didn't understand indentations fully. This isn't like any other languages. You need to make indentations correctly.
Hope it will help you 😃

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!

Entering a floating point number, not reaching the error message

I am trying to write a program for this assignment:
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
If the user enters a value out of range, print a suitable error message and exit.
But it doesn't print the sentence.
try:
inp = raw_input("Enter Score: ")
score = float(inp)
except:
print "Please enter a score number between 0.0 and 1.0"
quit()
if score >= 0.9 :
print "A"
elif score >= 0.8 :
print "B"
elif score >= 0.7 :
print "C"
elif score >= 0.6 :
print "D"
elif score < 0.6 :
print "F"
else:
print "Your score number is not in the 0 - 1 range."
score = input("Enter Score: ")
scor = float(score)
if 0.0 < scor > 1.0:
print("error")
elif scor >= 0.9:
print("A")
elif scor >= 0.8:
print("B")
elif scor >= 0.7:
print("C")
elif scor >= 0.6:
print("D")
elif scor < 0.6:
print("F")
You should raise the exception to get it printed. Here is the code changes that you are looking for.
try:
inp = raw_input("Enter Score: ")
score = float(inp)
if score > 1.0 or score < 0
raise
except:
print "Please enter a score number between 0.0 and 1.0"
quit()
if score >= 0.9 :
print "A"
elif score >= 0.8 :
print "B"
elif score >= 0.7 :
print "C"
elif score >= 0.6 :
print "D"
elif score < 0.6 :
print "F"
else:
#your logic
Hope this was helpful.
Pavan, your code doesn't pass the exam, because you accept values which are higher than 1.0 or smaller than 0.0 which is incorrect.
Add a check somewhere which validates that the score is between 0.0 and 1.0.
So like I highlighted in the comment section please make the modifications to your if statement like below:
try:
inp = raw_input("Enter Score: ")
score = float(inp)
except:
print "Please enter a score number between 0.0 and 1.0"
quit()
# change your if statement here:
if score > 1.0 or score < 0.0:
print "Your score number is not in the 0 - 1 range."
elif score >= 0.9 :
print "A"
elif score >= 0.8 :
print "B"
elif score >= 0.7 :
print "C"
elif score >= 0.6 :
print "D"
elif score < 0.6 :
print "F"
EDIT: First of all the if branch checks for an out of bound score which is a value above 1.0 and below 0.0, then prints an error message.
If the score is within limits than the other elif blocks check to value of the score as in the original post.
inp = input("Enter score between 0.0 and 1.0:")
score = float(inp)
if score >= 0.9 and score <= 1.0:
print("A")
if score >= 0.8 and score < 0.9:
print("B")
if score >= 0.7 and score < 0.8:
print("C")
if score >= 0.6 and score < 0.7:
print("D")
if score >= 0.0 and score <0.6:
print("F")
elif score > 1.0:
print("Error, please enter score between 0.0 and 1.0")
quit()
scr=(input("Enter the Score: "))
try:
scr=float(scr)
if scr>=0.0 and scr<=1.0:
if scr >= 0.9:
print("A")
elif scr >= 0.8:
print("B")
elif scr >= 0.7:
print("C")
elif scr >= 0.6:
print("D")
elif scr<0.6:
print("F")
else:
print("Out Of range")
except:
print("Try a number")
Explanation:
scr is the score which will take float as input ranging from 0.0 to 1.0. If the score is less than 0.0 or greater than 1.0 then it prints "Out of Range". If entered input is not float/ int then it will print "Try a Number".
inp = input("Enter a Score between 0.0 and 1.0")
score = float(inp)
if score >= 0.9 and score <=1.0:
print ('A')
elif score >= 0.8 and score <0.9:
print ('B')
elif score >= 0.7 and score <0.8:
print ('C')
elif score >= 0.6 and score <0.7:
print ('D')
elif score >= 0.0 and score <0.6:
print ('F')
elif score > 1.0 or score < 0.0:
print ("Error, the score should be in the range between 0.0 and 1.0")
quit()

Categories