Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

python - Python:为什么我的内部嵌套while循环继续无限执行(Python: Why does my inner-nested while-loop continue to execute indefinitely)

Python 3.4.3

(的Python 3.4.3)

I am trying to create an interactive drawing program using Python and turtle.

(我正在尝试使用Python和turtle创建一个交互式绘图程序。)

My program runs by first asking the user to specify the length of the sides of the shape.

(我的程序通过首先要求用户指定形状侧面的长度来运行。)

If the length is greater than zero, the program will begin to execute.

(如果长度大于零,程序将开始执行。)

I would like the program to continue running and to continue asking the user for information, up until the point the user enters the length of the sides as less than or equal to zero, at which point the program will exit.

(我希望程序继续运行并继续向用户询问信息,直到用户输入边的长度小于或等于零为止,然后程序将退出。)

I am therefore using a 'while-loop' to run the program and ask the user for specifics.

(因此,我正在使用“ while循环”来运行程序并询问用户具体信息。)

I want the user to be limited to requesting one of the following three shapes;

(我希望用户仅限于请求以下三种形状之一;)

octagon, heptagon or hexagon.

(八边形,七边形或六边形。)

If the user enters anything other than this, I want the program to ask the user to specify their choice of shape again.

(如果用户输入了其他内容,则我希望程序要求用户再次指定其形状选择。)

Therefore, I have the following code:

(因此,我有以下代码:)

import turtle
lineLength = float(input("What length line would you like? "))

while lineLength > 0 :
    print("You have choosen to draw a shape, please enter specifics:")
    penColor = input("Please choose a pen color: ")
    shape = input("which shape would you like to draw? ")

    while shape.lower() != "octagon".lower() or shape.lower() !=
    "heptagon".lower() or shape.lower() != "hexagon".lower() :
        print("You have to select a shape from the specified list.")
        shape = input("What shape would you like to draw: ")

Output: The actual output is that the code will run the "You have to select a shape from the specified list. What shape would you like to draw: " indefinitely;

(输出:实际输出是代码将运行“您必须从指定的列表中选择一种形状。您想绘制什么形状:”无限期;)

regardless of the input from the user.

(不管用户输入什么。)

Expected Output: My aim, and what I am expecting, is for the inner while-loop to exit once the user has input either octagon, heptagon or hexagon.

(预期的输出:我的目标和期望是,一旦用户输入八边形,七边形或六边形,则退出内部while循环。)

In fact, I do not understand why the inner while-loop should run at all if the user selects one of these three shapes as the condition for the while-loop has not been met.

(实际上,我不理解为什么如果用户选择这三种形状之一来满足while循环的条件,那么内部while循环为什么应该完全运行。)

  ask by Andrew Hardiman translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use and , not or .

(您需要使用and ,不or 。)

If you think about it, it's never possible that your loop will end, because no matter what the shape is, it will always not both of the other shape names.

(如果考虑一下,循环永远不会结束,因为无论形状是什么,它都不会同时出现其他两个形状名称。)

ie 'octagon' doesn't equal 'heptagon', so it will keep looping.

(即“八边形”不等于“七边形”,因此它将不断循环。)

If you change that to be and , it will only loop if the shape doesn't equal any of them, which is what you intend.

(如果将其更改为and ,则仅在形状不等于它们中的任何一个时才循环,这就是您想要的。)

De Morgan's laws is a good thing to read to get a better understanding of this kind of logic.

(阅读德摩根定律是一件好事,可以更好地理解这种逻辑。)

A cleaner way to do this is to use not in :

(一种更清洁的方法是在中not in使用:)

while shape.lower() not in ["octagon", "heptagon", "hexagon"] :
    print("You have to select a shape from the specified list.")
    shape = input("What shape would you like to draw: ")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...