简体   繁体   English

为什么我的WHILE循环无法在Python中运行?

[英]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. 我是一名初学者程序员,正在使用Python学习,但是我很确定自己对如何使大多数事情都运行有很好的了解,直到我发现了这一点。 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 . 当我尝试使用嵌套在其中的if..then语句运行一段代码时,Python决定不运行if ... then语句就丢给我一个曲线。 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. 当我尝试运行该程序时,它所做的只是连续地运行while循环内的一行代码,该代码位于if ... then语句之前。 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: 我为while循环尝试了其他一些变量,例如:

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 () . 您没有调用.lower()方法,因为没有() You're just setting query equal to the lower method of strings, so you're always taking the else branch. 您只是将query设置为等于字符串的lower 方法 ,因此您始终采用else分支。

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() ,而不是s.lower s.lower is a method. s.lower是一种方法。

What you're doing is you're assigning that method to query . 您正在做的是将那个方法分配给query So, none of the if s will ever match. 因此,所有if都不匹配。 This means the else branch is executed. 这意味着else分支已执行。 But, you don't print "Umm... I'm confused..." , you just have the string itself there. 但是,您不会print "Umm... I'm confused..." ,而您只在其中显示字符串本身。 This leads to you not getting any output. 这导致您没有得到任何输出。

couple of things 几件事

  1. lower is a function call, you should use it like lower() lower是一个函数调用,您应该像lower()一样使用它
  2. You are not printing the string in the else: statement. 您没有在else:语句中打印字符串。 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. 不要说while_variable == Truedeg_rad == False分别使用while_variablenot deg_rad使用while_variable (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> ). 您尚未调用lower方法,因此此时query 始终将是一个绑定的方法对象(类似于<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. 此外,在您的else子句中,您尚未使用print ,因此创建了字符串,然后将其丢弃而不显示。

Thus you get the raw_input, then nothing. 这样就得到了raw_input,然后什么也没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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