-2

Okay, so for my homework I need to write a function that will convert a score into a grade...

name = input("Hello, what is your name?\n")
print("Welcome", name)
mod1 = int(input("Please enter your module 1 result.\n"))
mod2 = int(input("Thank you, please enter your module 2 result.\n"))
print("Great")
grade = 0
grade2 = 0
if mod1 or mod2  >= 80:
    grade = "A"
    grade2 = "A"
elif mod1 or mod2  >=70:
    grade = "B"
    grade2 = "B"
elif mod1 or mod2 >=60:
    grade = "C"
    grade2 = "D"
elif mod1 or mod2 >=50:
    grade = "D"
    grade2 = "D"
elif mod1 or mod2 >=40:
    grade = "E"
    grade2 = "E"
elif mod1 or mod2 >=30:
    grade = "F"
    grade2 = "F"
elif mod1 or mod2 >=20:
    grade= "G"
    grade2= "G"
elif mod1 or mod2 >=10:
    grade= "U"
    grade2= "U"

print(grade, grade2)

Whenever I run this the results are : A,A ????

ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • What exactly doesn't work? Can you be a little more specific about the problem you're facing – Tim Apr 17 '14 at 14:27
  • What does it mean "it doesn't work"? – ElmoVanKielmo Apr 17 '14 at 14:27
  • Also, should you really be giving them the grade from the higher-scoring module twice? I would make this a function `def grade(mod):` then e.g. `grade1 = grade(mod1)` – jonrsharpe Apr 17 '14 at 14:29
  • 1
    @ElmoVanKielmo `input()` is the correct way when using Python 3, and the `print()` statements have the required parentheses for that version as well. I don't think that this is for Python 2. – Leigh Apr 17 '14 at 14:36
  • whoever downrated me twice...please tell me why? – user3545328 Apr 17 '14 at 14:39
  • @user3545328 is it Python 2 or Python 3? – ElmoVanKielmo Apr 17 '14 at 14:39
  • @ElmoVanKielmo python 3 – user3545328 Apr 17 '14 at 14:42
  • 1
    @user3545328 I'm guessing here (I didn't downvote), but I think it's because some variant of this question is asked about once every day on this website, so there are thousands of answers out there already. That would indicate you didn't do searching beforehand to see if someone had already solved your problem. Most users of this website see that as not following the rules and tend to downvote those types of questions. – SethMMorton Apr 17 '14 at 14:42

3 Answers3

1

Your boolean expressions are doing:

if (mod1) or (mod2 >= 80):
    # ....

This means that it is testing mod1 and mod2 > 80 for the boolean value True. Any number that is non-zero is considered "truthy" in Python (i.e. bool(2) == True; bool(0) == False) and as such your first if statement will always be triggered (unless you setmod1andmod2` to 0).

What you probably want instead is something along the lines of

if (mod1 >= 80) or (mod2 >= 80):
    # ...

Alternatively you could use any and a generator expression:

mod = [mod1, mod2]
if any(i >= 80 for i in mod):
    # ...

This will test all values of mod to see if they are greater than 80 and it any of them return True will cause the if statement to progress.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • I'd appreciate feedback from whomever down-voted me, that way I can improve my answer :) – Ffisegydd Apr 17 '14 at 14:31
  • I didn't downvote, but if I had to guess it could be either that you used `>` instead of `>=` as was in the original question, or because you used unnecessary `()` in your `if` statement. I don't know though, it could be for something else. – SethMMorton Apr 17 '14 at 14:35
  • 1
    The unnecessary brackets in the if statement are to clearly break up the two different boolean tests. I think the downvote was because when I originally answered it was sparse (too sparse really) but I have since edited it. – Ffisegydd Apr 17 '14 at 14:37
1
if mod1 or mod2  >= 80:

What this is actually saying is if (mod1) or (mod2 >= 80):. If mod1 has any values, it's True, and so this stops checking the rest and just executes the block in this if section.

Change it to if mod1 >= 80 or mod2 >= 80: and so on and it will work.

For why they're both the same, it's because you're checking from the highest grade downwards, and when one of the grades is satisfied by that condition then both are set to it. Step through the following:

# Using these input values.
mod1 = 10
mod2 = 81

# mod2 is greater than 80, we enter this block.
if mod1 >= 80 or mod2  >= 80:
    grade = "A"
    grade2 = "A"

# Now...
grade = "A"
grade2 = "A"
Leigh
  • 12,038
  • 4
  • 28
  • 36
  • ive now done this and it says mod is not indentified – user3545328 Apr 17 '14 at 14:32
  • 1
    @user3545328 Sounds like you accidentally wrote `mod` when you meant `mod1` or `mod2`. – SethMMorton Apr 17 '14 at 14:33
  • @SethMMorton yeah I did but it still gives two of the higher result – user3545328 Apr 17 '14 at 14:37
  • 1
    It will give two of the same values of the highest result because you're setting that manually, you aren't checking them independently. Use a loop to check both of the variables separately and that will allow them to get the correct scores. If `mod1` is 0 and `mod2` is 80 both of them are set to `A`. – Leigh Apr 17 '14 at 14:38
  • Or maybe you want `if mod1 >= 80 and mod2 >= 80:` instead? – SethMMorton Apr 17 '14 at 14:44
  • You're probably best off getting `mod1`, figuring out the grade, and then getting `mod2` after that, and figuring out that grade. If they both contribute to the grade for one subject, then get both as you do now and add them together, and do the check that way. Don't run the check for both of the grades at the same time, they're probably independent. – Leigh Apr 17 '14 at 14:49
0
Whenever I run this the results are : A,A ????

That is because here

if mod1 or mod2  >= 80:
    grade = "A"
    grade2 = "A"

you check if mod1 is not None, and because you just assigned a value to it with your input, it is indeed not None. Whatever you input, it will always enter the first if and give A, A as output.

Tim
  • 41,901
  • 18
  • 127
  • 145
  • 2
    `None` is not the only value that can be `False`. Other things that are `False` are `False`, `{}`, `[]`, `()`, `''`, and `0`. Checking if is not `None` only would be `if mod1 is not None:`. – SethMMorton Apr 17 '14 at 14:36