简体   繁体   中英

Python: Why does my inner-nested while-loop continue to execute indefinitely

Python 3.4.3

I am trying to create an interactive drawing program using Python and 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.

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

You need to use and , not 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. 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 :

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: ")

OR condition gets TRUE when at least one argument is TRUE

AND condition gets TRUE when all the arguments are TRUE

So, the possible methods are,

  • (shape == octagon) OR (shape == heptagon) OR (shape == hexagon)
  • (shape != octagon) AND (shape != heptagon) AND (shape != hexagon)

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