Learning python, lost with script


 
Thread Tools Search this Thread
Top Forums Programming Learning python, lost with script
# 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 or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

2. Shell Programming and Scripting

Learning to Script in Linux

Hello, I'm trying to branch out and learn Linux, but my comfort zone is PowerShell. I figure the best way to learn it is to do it so I moved my Plex Media Server to Ubuntu Server. What I'm trying to do is build a script that searches a directory and all subdirectories for files with the .ts... (5 Replies)
Discussion started by: Rhysers
5 Replies

3. What is on Your Mind?

Has Python Lost The Plot?

Not sure if this should be in the Programming forum so placed here for safety... I used to really love Python, but as it has evolved from V1.4.0, (for the classic AMIGA in my case), to its present V3.4.? incarnation has it become less user friendly for newbie and amateur coders? I have... (6 Replies)
Discussion started by: wisecracker
6 Replies

4. Shell Programming and Scripting

Learning project ideas - shell, python, UNIX tools, system administration

Hi guys, I am currently working as a system administration engineer, administering telecom applications on linux/unix platforms. I want to learn new things and improve the ones that i have and for this i though to really work on some project or something but i lack of ideas. I want to be... (2 Replies)
Discussion started by: capitanui
2 Replies

5. Programming

General question about learning Python

I am planning on taking a class in Python. My choices are 2.5 or 3.0. Which version should I choose? I am getting the impression they are two separate paths. thanks. (5 Replies)
Discussion started by: djehresmann
5 Replies

6. UNIX for Dummies Questions & Answers

I feel lost, how do I start learning UNIX?

I'm in college now and a part of a subject in this semester is learning UNIX, though the teacher failed to explain the basics, I feel, we got straight to the Unix Terminal and started putting in commands without much explanation what they were for. I quickly adapted to the basic ones, but in 5... (4 Replies)
Discussion started by: aco036
4 Replies

7. Programming

7 days into learning PYTHON & looking for some feedback

Hey folks. Title says it all, but... It's been an interesting few days. Never done anything object-oriented before. Previously only had BASH experience. I'm LOVING python. I see so much potential (of course). Can't wait to really get a feel for what's available in the Standard Library. I have no... (6 Replies)
Discussion started by: ryran
6 Replies

8. UNIX for Dummies Questions & Answers

learning how to script

I have this command line entry that I would like to turn into a script. I want to be able to just run the script with options for the report name and switch. Can anyone help me get started? I am hoping that by creating this script it will help me be able to create more. I have never written one. I... (11 Replies)
Discussion started by: llsmr777
11 Replies

9. Shell Programming and Scripting

learning how to use shell script

hello everyone, i am still trying to get this script to work, but with no luck. It is a little beyond my knowledge of scripting at the moment. The beginner book i have has an exercise listed that asks me to write a script tha allows for user input. For example " what is your name: " and then you... (3 Replies)
Discussion started by: bebop1111116
3 Replies
Login or Register to Ask a Question