#Hey im just after some help getting the hang of if statements thanks in advance :)

27 messages · Page 1 of 1 (latest)

sterile jewel
#
score = input("Enter Score: ")
float(score)


#Check score against conditions
if score < 0.6:
    print("F")
elif score >= 0.6:
    print("D")
elif score >= 0.7:
    print("C")
elif score >= 0.8:
    print("B")
elif score >= 0.9:
    print("A")
else:
    print("Error")
shell dock
#

Since 0.7 > 0.6 the first elif will catch 0.7 and not let it get to the correct elif. Same goes for all of your elif's. You have to organize it so that the least general condition is first and the most general is last.

#

So you're gonna need to reorganize the elif's in the reverse order that you have them.

sterile jewel
#

"the first elif will catch 0.7 and not let it get to the correct elif." Thanks for reply zach what do you mean by this sorry

shell dock
#

The if and all of it's elif's only run up until one of them evaluates to true. So if score is 0.83 this happens

  • is 0.83 < 0.6? False
  • is 0.83 >= 0.6? True
  • print "D"
#

See how it never gets to the 0.7 check or the 0.8 check?

#

It stops at the first one that is true.

sterile jewel
#

ahhh okay that makes sense to me

#

i dont think i made it that far though before aha i was getting this error back

#

TypeError: '>=' not supported between instances of 'str' and 'float'

shell dock
#

Ah I see that one now.

#

You can't just call float you've gotta assign it to something

sterile jewel
#

but you helped me immensly before i made to it aha

shell dock
#

So you gotta do something = float(something)

sterile jewel
#

i think i have assigned float to the variable score but im not to sure now

shell dock
#

You haven't. It's all by its lonesome on that line. Which is allowed in Python, just in this case it does nothing.

sterile jewel
#

how could i go about returning the input value and converting into the float

#
float(score)```
shell dock
#

Just assign the return from float to a variable.

#

So score = float(score)

#

Just change the one line to have that = bit

sterile jewel
#

how does that get the value from my input though

shell dock
#

You put the value from the input into score, float then changes it from a string to a number.

sterile jewel
#

my bad

shell dock
#

float doesn't actually change score though, it just returns the number, so to change score you need to reassign it the return from float

score = input("Enter Score: ")  # Score is a string
score = float(score)  # Score is now a number
sterile jewel
#

dude thank you so much manjm

#

i owe you a drink enjoy the rest of your weekend!