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")
Asked
Active
Viewed 929 times
-1

tynn
- 38,113
- 8
- 108
- 143

Jennifer Taylor
- 47
- 2
-
that is what `elif` is for – Pynchia Jun 22 '15 at 07:38
-
1Related: http://stackoverflow.com/questions/12774279/python-how-to-check-if-a-variable-is-equal-to-one-string-or-another-string – Łukasz Rogalski Jun 22 '15 at 07:47
-
Since you're not using the value of `i` in `for i in range(...)`, I'd suggest using `for _ in range(...)`. Using an underscore as the loop variable is a common convention which emphasises that something is being repeated, and that the loop variable isn't important. – alexwlchan Jun 22 '15 at 12:47
3 Answers
3
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")

Delimitry
- 2,987
- 4
- 30
- 39
-
-
2Wouldn't `shape = input("Give me a shape: ").lower()` be an improvement rather than changing the case for every condition? – cdarke Jun 22 '15 at 08:25
1
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"

tynn
- 38,113
- 8
- 108
- 143
0
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")
-
When I do that it does the first one and then none of the others. For example if I type circle it will only print out the pentagon and then stop. – Jennifer Taylor Jun 22 '15 at 07:42
-
That is beacsue the or "Pentagon" will always be True as you aren't re-evaluating shape. Please see my edit. – Rob Jun 22 '15 at 07:51