Python Forum
conditional assignment failing
Thread Rating:
  • 3 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conditional assignment failing
#1
I am tying to do a conditional assignment, and it isn't working. I can't work out what I've done wrong. Any suggestions would be very gratefully received. I can enter a score (eg 0.95) and it prints. But then fails on the conditional if expression.

score = float( input( ' Enter Score between 0.0 and 1.0: ' ) )
print('Score: ' , score)
if score >= 0.9 and < 1.0 :
	print('Score: A')
Thanks, Charlotte
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#3
Try this expression:

0.0 <= score <= 1.0
Meaning: 0.0 is lesser or equal than score is lesser or equal than 1.0.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Thanks for the answers. I was misinterpreting 'and' (as I think you realised buran). It's going to take some time for all this to sink in I think. However, I've finally managed to get my script to do what I want it to.

score = float( input( ' Enter Score between 0.0 and 1.0: ' ) )
print('Score: ' , score)
if score >= 0.9 <= 1.0 :
print('Score: A')
elif score >=0.8 < 0.9 :
print('Score: B')

Meaning if the user input score is greater than or equal to 0.9 or less than or equal to 1.0 print 'Score: A'. If user input score is greater than or equal to 0.8 or less than 0.9 print 'Score: B'. And so on.

It now does what I want it to do.

Thanks for taking the time to reply.
Reply
#5
(Aug-07-2017, 04:00 PM)charlottecrosland Wrote: It now does what I want it to do.
That elif looks very strange.  Sure, it checks if the 0.8 is less than both the score and 0.9, but you already know that the score is less than 0.9 (because otherwise the first if block would have captured it).

Instead of doing ranges, I think it'd look cleaner if you did simple checks:
score = float(input("Score: "))
if score > 1.0:
    print("Invalid score")
elif score >= 0.9:
    print("A")
elif score >= 0.8:
    print("B")
Reply
#6
well, not exactly - if you enter score greater than 1.0 it will still print A.
it should be like
 
while True:
    score = float( input( ' Enter Score between 0.0 and 1.0: ' ) )
    print('Score: ' , score)
    if score < 0 or score > 1:
        print('this is not a valid score')
        continue # return to the begining of loop /continue with next iteration
    elif score >= 0.9:
        print('Score: A')
    elif score >= 0.8:
        print('Score: B')
    # you can continue further
    break # exit the loop
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to Build a Calculator Yotbar 3 2,147 Apr-07-2020, 04:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020