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")
If statements in python. Does not stop the other if statements from working
975 views Asked by Jennifer Taylor At
3
There are 3 answers
1
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")
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.