简体   繁体   中英

Why won't my WHILE loops run in Python?

I'm a beginner programmer, learning in Python, but I was pretty sure I had a decent grasp of how to make most things run, until I came across this. As i was attempting to run a piece of code with an if..then statement nested inside, Python decided to throw me a curve ball by not running the if...then statements . When I try to run the program, all it does is continually run the one line of code I have inside the while loop, coming before the if...then statement. Here's the code:

def deg_or_rad():
    global deg_rad
    deg_rad = False

    while deg_rad == False:
        query = raw_input("Are you working in 'degrees' or 'radians'?  > ").lower
        if query == "deg" or \
           query == "degrees":
            deg_rad = "deg"
            print "Cool! I like degrees."
        elif query == "rad" or \
             query == "radians":
            deg_rad = "rad"
            print "Cool! I like radians."
        else:
           "Umm... I'm confused..."

I've tried a few other variables for the while loop, such as:

def deg_or_rad():
    global deg_rad
    deg_rad = False
    while_variable = True
    while while_variable == True:
        query = raw_input("Are you working in 'degrees' or 'radians'?  > ").lower
        if query == "deg" or \
           query == "degrees":
            deg_rad = "deg"
            print "Cool! I like degrees."
            while_variable = False
        elif query == "rad" or \
             query == "radians":
            deg_rad = "rad"
            print "Cool! I like radians."
            while_variable = False
        else:
           "Umm... I'm confused..."

Anyone have any ideas? I'm really confused by this point.

First, in this line:

    query = raw_input("Are you working in 'degrees' or 'radians'?  > ").lower

you're not calling the .lower() method, because there are no () . You're just setting query equal to the lower method of strings, so you're always taking the else branch.

Second, in this line:

       "Umm... I'm confused..."

You're not printing anything, you just made a string. So even though that branch is being taken, you're not doing anything you can see.

This is a combination of two things that make it seem like nothing is happening.

To lower a string, you do s.lower() , not s.lower . s.lower is a method.

What you're doing is you're assigning that method to query . So, none of the if s will ever match. This means the else branch is executed. But, you don't print "Umm... I'm confused..." , you just have the string itself there. This leads to you not getting any output.

couple of things

  1. lower is a function call, you should use it like lower()
  2. You are not printing the string in the else: statement. So it appears like nothing is happening even though you are getting here. This is because that function itself will not match your previous conditions (the results of the function may)
  3. Don't say while_variable == True or deg_rad == False just use while_variable or not deg_rad respectively. (This isn't really part of the problem, just bad style.)
  4. You could trying printing things to try and debug where your function is deviating from expected behavior to try and narrow it down. For instance if you put in a debug print just after you capture query input you could see that it wasn't what you were hoping for.

example:

def deg_or_rad():
    global deg_rad
    deg_rad = False

    while not deg_rad:
        query = raw_input("Are you working in 'degrees' or 'radians'?  > ").lower()
        if query in ("deg", "degrees"):
            deg_rad = "deg"
            print "Cool! I like degrees."
        elif query in ("rad", "radians"):
            deg_rad = "rad"
            print "Cool! I like radians."
        else:
            print "Umm... I'm confused..."
 raw_input("Are you working in 'degrees' or 'radians'? > ").lower 

You haven't called the lower method, so at this point query is always going to be a bound method object (something along the lines of <built-in method lower of str object at 0x1001a0030> ).

Furthermore, in your else clause you have not used print , so the string is created then thrown away without being displayed.

Thus you get the raw_input, then nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM