Unix/Linux Go Back    

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

Learning python, lost with script


Thread Tools Search this Thread Display Modes
Old Unix and Linux 10-10-2015   -   Original Discussion by la2015
la2015's Unix or Linux Image
la2015 la2015 is offline
Registered User
Join Date: Oct 2015
Last Activity: 17 October 2015, 7:55 AM EDT
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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:

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


        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"


            print "bad score"

whereas this other one does do the job correctly:

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


    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"

    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?
Sponsored Links
Old Unix and Linux 10-12-2015   -   Original Discussion by la2015
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
Join Date: Apr 2009
Last Activity: 16 June 2018, 10:15 PM EDT
Location: India
Posts: 1,984
Thanks: 31
Thanked 575 Times in 548 Posts
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
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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

All times are GMT -4. The time now is 01:22 PM.