Unix/Linux Go Back    


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

Learning python, lost with script

Programming


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
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:


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?
Sponsored Links
    #2  
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: 8 November 2017, 2:32 AM EST
Location: India
Posts: 1,976
Thanks: 31
Thanked 574 Times in 547 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
Closed

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 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
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



All times are GMT -4. The time now is 10:01 AM.