I am trying to create a simple grade program in which the user is prompted for a decimal grade and in return are given a letter grade. It just doesn't seem to work. When I place a number like .9 it gives me B. It gives me B for all inputs and when I test it with a number greater than 1, it gives me "B A D C".
try :
grade = raw_input("what is ur grade")
if grade > .8 < .9 :
print "B"
if grade > .9 < 1.0 :
print "A"
if grade > .6 < .7 :
print "D"
if grade > .7 < .8 :
print "C"
if grade < .6 :
print "F"
except :
if grade > 1.0 :
print "enter numeric value"
Please Help me understand...
from bisect import bisect
def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
i = bisect(breakpoints, score)
return grades[i]
while True:
try:
grad = float(raw_input("what is ur grade")) *100
print grade(grad)
break
except:
print 'Enter Correct value'
explantion for your code:
grade = raw_input("what is ur grade")
here your grade type is always string you need to convert it into float
if grade > .8 < .9
i think your grade between .8 and .9
it can be done
if .8<grade<.9:
Related
I'm just getting started out with python and while practicing functions to print grade of a student whose name and score I input, it runs without any errors.
I just am not able to get it print the name and grades no matter what I do.
The code:
namee=input('What is your name: ')
scoree=float(input("What is your grade: "))
def markz(name,score):
if score >= 9.0 and score <= 9.9: return "A"
elif score >=8.0 and score <= 8.9: return "B"
elif score >=7.0 and score <= 7.9: return "C"
elif score >=6.0 and score <= 6.9: return "D"
elif score <=5.0 : return "F"
else: return "Invalid Grade"
print("Hello ,"+name+". You are graded ",score)
markz(namee,scoree)
I want the output to be like, Hello, xyz. You are graded B
Basically, you have to return markz() in the print statement. Also, I've simplified your code a bit.
Try this:
def markz(score):
if 9.0 <= score <= 9.9:
return "A"
elif 8.0 <= score <= 8.9:
return "B"
elif 7.0 <= score <= 7.9:
return "C"
elif 6.0 <= score <= 6.9:
return "D"
elif score <= 5.0:
return "F"
else:
return "Invalid Grade"
namee = input('What is your name: ')
scoree = float(input("What is your grade: "))
print("Hello , " + namee + ". You are graded ", markz(scoree))
Sample output:
What is your name: Foo
What is your grade: 8
Hello , Foo. You are graded B
The reason it wasn't working for you was that the print("Hello ,"+name+". You are graded ",score) is simply unreachable. You've put it after the last return so it would never got executed.
My code:
def askTestScores():
test1 = int(input("Enter test 1 score: "))
test2 = int(input("Enter test 2 score: "))
test3 = int(input("Enter test 3 score: "))
test4 = int(input("Enter test 4 score: "))
test5 = int(input("Enter test 5 score: "))
testScores = (test1,test2,test3,test4,test5)
return testScores
def determine_grade(testScores):
for i in range(testScores):
if i >= 90 and i <= 100:
return "A"
elif i >= 80 and i <= 89:
return "B"
elif i >= 70 and i <= 79:
return "C"
elif i >= 60 and i <= 69:
return "D"
else:
return "F"
def displayGrades(testScores):
print("Score\t\t Grade")
print("-----------------------")
print(f"{testScores[0]}\t\t {determine_grade(testScores[0])}")
print(f"{testScores[1]}\t\t {determine_grade(testScores[1])}")
print(f"{testScores[2]}\t\t {determine_grade(testScores[2])}")
print(f"{testScores[3]}\t\t {determine_grade(testScores[3])}")
print(f"{testScores[4]}\t\t {determine_grade(testScores[4])}")
x = askTestScore
displayGrades(x)
This is only printing "F" when I use the range function in the loop for determine_grade function. What I want is, to print each grade individually, but when I remove the range function I am getting type error: saying int object not iterable. How can I solve this?
instead of range() Use:
for i in testscores :
Why do you need for in determine_grades ?
I think you meant this
def determine_grades(testScore):
if testScore >= 90 and testScore <= 100:
return "A"
elif testScore >= 80 and testScore <= 89:
return "B"
elif testScore >= 70 and testScore <= 79:
return "C"
elif testScore >= 60 and testScore <= 69:
return "D"
else:
return "F"
def displayGrades(testScores):
print("Score\t\t Grade")
print("-----------------------")
for testScore in testScores:
print(f"{testScore}\t\t {determine_grades(testScore)}")
Here you loop testScores in displayGrades to pass one score at a time to determine_grade and print them.
Because if you used for loop in determine_grades then you need to return a list of grades and not String.
You can improve make your code by appending the inputs to a list instead of tuple
def askTestScores():
testScores = []
for i in range(5):
score = int(input(f"Enter test {i + 1} score: "))
testScores.append(score)
return testScores
EDIT:
If you need tuple you can convert the list to tuple
def askTestScores():
testScores = []
for i in range(5):
score = int(input(f"Enter test {i + 1} score: "))
testScores.append(score)
testScores = tuple(testScores)
return testScores
the easiest way to do it is to include each item of your tuple in a list and remove the range function:
def askTestScores():
test1 = int(input("Enter test 1 score: "))
test2 = int(input("Enter test 2 score: "))
test3 = int(input("Enter test 3 score: "))
test4 = int(input("Enter test 4 score: "))
test5 = int(input("Enter test 5 score: "))
testscores = ([test1],[test2],[test3],[test4],[test5])
return testscores
def determine_grade(testscores):
for i in testscores:
if i >= 90 and i <= 100:
return "A"
elif i >= 80 and i <= 89:
return "B"
elif i >= 70 and i <= 79:
return "C"
elif i >= 60 and i <= 69:
return "D"
else:
return "F"
def displayGrades(testscores):
print("Score\t\t Grade")
print("-----------------------")
print(f"{testscores[0]}\t\t {determine_grade(testscores[0])}")
print(f"{testscores[1]}\t\t {determine_grade(testscores[1])}")
print(f"{testscores[2]}\t\t {determine_grade(testscores[2])}")
print(f"{testscores[3]}\t\t {determine_grade(testscores[3])}")
print(f"{testscores[4]}\t\t {determine_grade(testscores[4])}")
x = askTestScores()
displayGrades(x)
displayGrades(x)
#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!
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()
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