0

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.

2 Answers 2

2

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. i.e. '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: ")
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. This works. However, my brain cannot understand the logic! I want the while-loop to execute if 'shape' is not equal to A or B or C.
I really recommend that you read about De Morgan's laws and play round with some Boolean Algebra to try and get a better understanding of exactly what your statements mean. There is a difference between how logic is expressed in english and it's meaning to the computer.
Thank you JavaNut. I appreciate the guidance. I suppose to some extent learning the syntax etc. is the easy part. I see what you are saying, and I’ll definitely take a look at De Morgan’s law.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.