New to Python and coding in general, can anyone help with my code?


I want the user to input their percentage and it will give them a grade, but when putting in anything other than a number it will give you an A* or a U. I assume this is because it puts numbers in the ‘alphabet’ along with letters above it and punctuation below it, so the >= signs are messed up. I’m not sure how to fix this, can anyone help.
Also, I am very knew to all of this, so any other tips/help would be appreciated!

By nikko via Discussions on Python.org at 27Apr2022 09:20:

Please post code inline as text, rather than as a screenshot.
Screenshots are not provided to users using email and offer challenges
to the visually impaired.

We can also copy/paste your code if it is text. Code should appear
between tripl backticks, eg:

```
your code here
```

Anyway, to your code…

I want the user to input their percentage and it will give them a grade, but when putting in anything other than a number it will give you an A* or a U. I assume this is because it puts numbers in the ‘alphabet’ along with letters above it and punctuation below it, so the >= signs are messed up.

You are correct in that assumption.

This is because you are working in strings.

Your code begins like this:

score = input("What was your percentage?")
if score >= "90":
    print("You got an A*")

The return from input() is a string (text). And your comparison is
also to a string: the expression "90" is a piece of text, not a
number.

To get correct behaviour you need score to be a number. For example,
this:

score = int(input("What was your percentage?"))

The takes the return string from input() and converts it to an integer
(the int type in Python). If the input is not a value integer the
int() call will raise an exception - this is not handled here, but you
can address that later.

Then you can perform a numeric test in your if-statement:

if score >= 90:

The expression 90 is an integer, as opposed to the expression "90"
which is a string.

This should get you on the way to working code.

If you need to work with fraction percentages such as 91.5, use
float instead of int.

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes
import operator as op
try:
  match (x := float(input('What was your percentage'))):
    case _ if op.and_(op.ge(x, 90), op.le(x, 100)): print(f'{x} => A+')
    case _ if op.and_(op.ge(x, 80), op.lt(x, 90)): print(f'{x} => A')
    case _ if op.and_(op.lt(x, 40), op.gt(x, 0)): print(f'{x} => U')
    case _ if op.or_(op.gt(x, 100), op.lt(x, 0)): print(f'invalid percentage')
except (ValueError): raise Exception('required integer or float')

Ah I see, you have to convert the numbers into integers instead of a string.
Thanks a lot, I will keep the screenshot thing in mind.

You’ve maybe got your answer by now, but building on the code that you posted, this is an example of how that could be adapted:

score = input("What was your percentage? ")

try:
    your_score = float(score)

    if your_score >= 90:
        your_grade = "A*"

    elif your_score >= 80:
        your_grade = "A"

    elif your_score >= 70:
        your_grade = "B"

    elif your_score >= 60:
        your_grade = "C"

    elif your_score >= 50:
        your_grade = "D"

    elif your_score >= 40:
        your_grade = "E"

    else: your_grade = "U"

except ValueError:
    print("Please enter your score as a number. e.g: if you got 87.4% enter 87.4 at the prompt.")
    your_grade = ""

if your_grade != "":
    print("Your grade is: "+your_grade)