Learning python, lost with script

Tags
programming

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 10-10-2015
Learning python, lost with script

Hi there,

im just having a hard time understanding why this code does not print anything that is suppose to print:

Code:
score = raw_input ('what is your score? \n')

try:

        if 1.0 == float(score) >= 0.9:
            print "A"

        elif 0.9 > float(score) >= 0.8:
            print "B"

        elif 0.8 > float(score) >= 0.7:
            print "C"

        elif 0.7 > float(score) >= 0.6:
            print "D"
    
        elif 0.6 > float(score):
            print "F"


except:

            print "bad score"

whereas this other one does do the job correctly:

Code:
score = raw_input ('please provide a score between 0.0 and 1.0\n')

try:

    if float(score) >= 0.9 and float(score) <= 1.0:

        print "A"  

    elif float(score) >= 0.8 and float(score) < 0.9:

        print "B"

    elif float(score) >= 0.7 and float(score) < 0.8:

        print "C"

    elif float(score) >= 0.6 and float(score) < 0.7:

        print "D"

    elif float(score) < 0.6:

        print "F"

except:
    print "Bad score"

Any insight?

---------- Post updated at 08:55 AM ---------- Previous update was at 08:44 AM ----------

Ok, I got it, the right way to define a numerical range is as in the second option. But why then I dont get any error if the first code is not written properly?
# 2  
Old 10-12-2015
Because for python it's a perfectly valid expression.

When you say, 1.0 == float(score) >= 0.9,
python understands it as 1.0 == float(score) and float(score) >= 0.9
which will always evaluate to false unless score is 1.0
Login to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Similar Threads More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Learning to Script in Linux Rhysers Shell Programming and Scripting 5 05-27-2018 04:11 AM
Has Python Lost The Plot? wisecracker What is on Your Mind? 6 08-11-2014 05:05 PM
Learning project ideas - shell, python, UNIX tools, system administration capitanui Shell Programming and Scripting 2 10-17-2013 06:52 AM
General question about learning Python djehresmann Programming 5 08-12-2013 04:29 AM
Lost in shell script AdamSahp Homework & Coursework Questions 7 03-24-2012 07:06 PM
I feel lost, how do I start learning UNIX? aco036 UNIX for Dummies Questions & Answers 4 02-20-2012 09:52 PM
7 days into learning PYTHON & looking for some feedback ryran Programming 6 12-16-2011 12:12 PM
Unix shell script learning sam70 UNIX Desktop Questions & Answers 9 07-20-2010 11:27 PM
New to shell script and lost.... LRoberts UNIX for Dummies Questions & Answers 8 10-16-2008 01:18 PM
Learning Tips - UNIX Shell Script Govindh.v UNIX for Dummies Questions & Answers 3 08-14-2008 07:51 PM
Script Language Parser for Learning kcampb9 UNIX for Dummies Questions & Answers 3 03-30-2008 02:21 PM
learning how to script llsmr777 UNIX for Dummies Questions & Answers 11 06-28-2007 01:01 PM
learning how to use shell script bebop1111116 Shell Programming and Scripting 3 09-18-2006 11:18 AM
Lost Data Lost Admin murphsr Filesystems, Disks and Memory 3 09-07-2005 03:35 PM
All times are GMT -4. The time now is 09:17 PM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?