Here was the exercise i had to do, however I tried some modifications but i failed in it. I wanted to do so that you can put yourself the mark you got, and after it, it will return your letter note value. Probably is a noob mistake, but i have tried a lot of things and still dont work. Could someone tell me where did I confuse, and how would be the right way to do it?
grade = raw_input (“Whats your mark?”)
def grade_converter(grade):
if grade >= 90:
return “A”
elif grade < 90 and grade >= 80:
return “B”
elif grade < 80 and grade >= 70:
return “C”
elif grade < 70 and grade >= 65:
return “D”
elif grade < 65 and grade >= 0:
return “F”
else:
return “Isnt a valid note”
print "you have gotten a " + grade_converter(grade)
Yeah, there is a problem with the link in @stevencopeland’s post. Assuming you have indented properly, your main issue is that the value assigned to your grade variable is a string. You can’t compare a string to an integer in your grade_converter function. Converting grade to an integer with the int() method will fix that. Your function will work, but it isn’t necessary to check the between-ness of the grade. You could simply check if the grade is greater than the uppermost value for each subsequent grade like so:
if grade > 89:
return "A" #the return statement will exit the function, so no need for a less than comparison
elif grade > 79:
return "B"
#and etc.
Hope this helps!
I’ve included your code with my suggestions here if you’d like to take a peek:
SPOILER ALERT!!
grade = int(raw_input("What's your mark? "))
def grade_converter(grade):
if grade > 89:
return "A"
elif grade > 79:
return "B"
elif grade > 69:
return "C"
elif grade > 59:
return "D"
elif grade >= 0:
return "F"
else:
return "Isn't a valid note"
print"You have gotten a " + grade_converter(grade)