If statements in python. Does not stop the other if statements from working

950 views Asked by At
import turtle

print("Give me a shape")
shape = input()

if shape == "pentagon" or "Pentagon":
    for i in range(5):
        turtle.fd(100)
        turtle.rt(72)

if shape == "triangle" or "Triangle":
    for i in range(3):
        turtle.fd(100)
        turtle.rt(120)

if shape == "square" or "Square":
    for i in range(4):
        turtle.fd(100)
        turtle.rt(90)

if shape == "hexagon" or "Hexagon":
    for i in range(6):
        turtle.fd(100)
        turtle.rt(60)

if shape == "circle" or "Circle":
    turtle.circle(100)

else:
    print("Not a shape")
3

There are 3 answers

2
Rob On

You should use elif statements after the first if. Alternatively you could convert your input to lower case by using '.lower'. Which would mean that you wouldn't have to have an or in the statements. It would also accept TRIANGLE for example.

import turtle

print("Give me a shape")
shape = input()

if shape == "pentagon" or shape == "Pentagon":
    for i in range(5):
        turtle.fd(100)
        turtle.rt(72)

elif shape == "triangle" or shape == "Triangle":
    for i in range(3):
        turtle.fd(100)
        turtle.rt(120)

elif shape == "square" or shape == "Square":
    for i in range(4):
        turtle.fd(100)
        turtle.rt(90)

elif shape == "hexagon" or shape == "Hexagon":
    for i in range(6):
        turtle.fd(100)
        turtle.rt(60)

elif shape == "circle" or shape == "Circle":
    turtle.circle(100)

else:
    print("Not a shape")
1
Delimitry On

Your check shape == "pentagon" or "Pentagon" is incorrect and will always be True, because checking string always return True, e.g. bool("Pentagon") is True and checking of "pentagon" == "pentagon" is True.
You should use shape in ["pentagon", "Pentagon"] or better shape.lower() == "pentagon" instead.

import turtle

print("Give me a shape")
shape = input().lower()  
if shape == "pentagon": 
    for i in range(5): 
        turtle.fd(100) 
        turtle.rt(72)    
elif shape == "triangle": 
    for i in range(3): 
        turtle.fd(100) 
        turtle.rt(120)    
elif shape == "square": 
    for i in range(4): 
        turtle.fd(100) 
        turtle.rt(90)    
elif shape == "hexagon": 
    for i in range(6): 
        turtle.fd(100) 
        turtle.rt(60)    
elif shape == "circle": 
    turtle.circle(100)    
else: 
    print("Not a shape")
0
tynn On

Your statements like

shape == "pentagon" or "Pentagon"

evaluate to True or "Pentagon".

You need to compare shape to both values:

shape == "pentagon" or shape == "Pentagon"