0

How do I get an input from the user to be used as grade?

def grade_converter(grade):
        if grade >= 90:
            return "A"
        elif grade >= 80:
            return "B"
        elif grade >= 70:
         return "C"
        elif grade >= 65:
            return "D"
        else:
            return "F"
  • `grade_converter` accepts an integer, so you'll have to turn the string you get from `raw_input` into an integer and then send it to `grade_converter`. – Patrick Haugh Aug 08 '18 at 20:27
  • Welcome to SO!. I did a search for how to read user input in python and I came across the following post with the title `Python: user input and commandline arguments` here https://stackoverflow.com/questions/70797/python-user-input-and-commandline-arguments. I hope it helps. – CEGRD Aug 08 '18 at 20:35

2 Answers2

3

raw_input() returns a string, but your function is comparing against integers. Convert your input to an integer before calling grade_converter():

grade = int(raw_input('Enter a grade'))
print grade_converter(grade)

Note that the conversion will raise an exception if you enter something that can't be converted to an integer.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
1

In Python3, raw_input() was renamed to input(). You could just use it as an input to your grade_converter() function:

if __name__ == '__main__':
    print('Enter a score')
    print('The grade is: ', grade_converter(int(input())))
jhill515
  • 894
  • 8
  • 26