Home Man
Search
Today's Posts
Register

Post questions about C, C++, Java, SQL, and other programming languages here.

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

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 03:11 AM
Has Python Lost The Plot? wisecracker What is on Your Mind? 6 08-11-2014 04:05 PM
Learning project ideas - shell, python, UNIX tools, system administration capitanui Shell Programming and Scripting 2 10-17-2013 05:52 AM
General question about learning Python djehresmann Programming 5 08-12-2013 03:29 AM
I feel lost, how do I start learning UNIX? aco036 UNIX for Dummies Questions & Answers 4 02-20-2012 08:52 PM
7 days into learning PYTHON & looking for some feedback ryran Programming 6 12-16-2011 11:12 AM
Unix shell script learning sam70 UNIX Desktop Questions & Answers 9 07-20-2010 10:27 PM
learning how to script llsmr777 UNIX for Dummies Questions & Answers 11 06-28-2007 12:01 PM
learning how to use shell script bebop1111116 Shell Programming and Scripting 3 09-18-2006 10:18 AM


All times are GMT -4. The time now is 05:31 AM.

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