I tried making Grade Converter with loops [Python3.7]

Hello everyone!

So I tried writing a simple program based on the grade converter from one of the exercises in Codecademy’s Python course.

The converter on Codecademy was supposed to convert points to a grade and then print the result. I wanted to go a little further and change it to do these things:

  • make sure the user inputs a number and have an error message pop up if the user inputs something else than a number

  • make it loop, that is: ask the user if they wish to continue converting [N] or quit[Q]

  • ask the user again, shall they input something else than [N] nor [Q]

  • (I also changed the syntax to Python 3.7 from Codecademy’s 2.sth)

The code does what I wanted it to do, which is a big success for me, but I would like to ask for opinions. Is there anything I could do better? Are there any unnecessary bits of code that shouldn’t be there or could have been more clear and simple?

There’s also one thing that has been bugging me. In Codecademy’s lesson, the function that determines the grade seemed to work when written like this:

def converter(x):
  if x >= 90:
    return "A"
# ...etc....
x = input("Your points:")
# ...etc...

that didn’t seem to work on my computer. I found a workaround using int(x). Is this because user input is a string and therefore cannot be compared to a number? If so, why did it work on Codecademy? Maybe I am overlooking something?

Anyway, here’s the whole code!

def grade_converter(x):
  if not x.isdigit():
    print("That was not a number!")
    return "a big unknown"
  elif int(x) >= 90 and x.isdigit():
    return "A"
  elif int(x) >= 80 and x.isdigit():
    return "B"
  elif int(x) >= 70 and x.isdigit():
    return "C"
  elif int(x) >= 65 and x.isdigit():
    return "D"
  else:
    return "F"


print("Welcome to Grade Calculator.\nIt helps you calculate the grade on your test, so you don\'t have to.")

grade = input("How many points did you get?:")
print("Your grade is %s." % (grade_converter(grade)))
answer = input("To keep calculating press [N], to exit press [Q]")


#loop for new calculation 
while answer.lower() in ["n", "q"]:
  while answer.lower() == 'n':
    grade = input("How many points did you get?:")
    print("Your grade is %s." % (grade_converter(grade)))
    answer = input("To keep calculating press [N], to exit press [Q]")
  while answer.lower() not in ["n", "q"]:
    print("I'm not sure what that was.")
    answer = input("Please pick [N]ew or [Q]uit.")
  if answer.lower() == 'q':
    print("Sad to see you go.")
    break

# loop that asks to pick n or q again
while answer.lower() not in ["n", "q"]:
  print("I'm not sure what that was.")
  answer = input("Please pick [N]ew or [Q]uit.")
  while answer.lower() not in ["n", "q"]:
    print("I'm not sure what that was.")
    answer = input("Please pick [N]ew or [Q]uit.")
  while answer.lower() in ["n", "q"]:
    while answer.lower() == 'n':
      grade = input("How many points did you get?:")
      print("Your grade is %s." % (grade_converter(grade)))
      answer = input("To keep calculating press [N], to exit press [Q]")
    if answer.lower() == 'q':
      print("Sad to see you go.")
      break
    

input("Press ENTER to exit")

Thanks in advance for any feedback. Much appreciated!

in the lessons, you never prompt for input? You just hard code integers into the program

you have a lot of repeat in your code, programming should be DRY (don’t repeat yourself)

for example, this:

elif int(x) >= 90 and x.isdigit():

the and x.isdigit() is redundant, you already verified x is a digit

ideally, use some more functions, this makes your program easier to write test for, and the function names can describe the task of the function which improves readability.