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")
-1
A
回答
0
您应该在第一个if后使用elif语句。或者,您可以使用'.lower'将输入转换为小写。这意味着你不必在陈述中或者在陈述中。例如,它也会接受TRIANGLE。
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")
+0
当我这样做,它做了第一个,然后没有其他人。例如,如果我输入圆圈,它只会打印五边形,然后停止。 –
+0
因为你不重新评估形状,所以或者“五角大楼”永远是真的。请参阅我的编辑。 – Rob
3
你的检查shape == "pentagon" or "Pentagon"
不正确,将永远是True
,因为检查字符串总是返回True
,例如bool("Pentagon")
是True
并且"pentagon" == "pentagon"
的检查是True
。
您应该改用shape in ["pentagon", "Pentagon"]
或更好的shape.lower() == "pentagon"
。
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")
1
你之类的语句
shape == "pentagon" or "Pentagon"
评估为True
或"Pentagon"
。
,则需要比较shape
这两个值:
shape == "pentagon" or shape == "Pentagon"
相关问题
- 1. 如果JavaScript中的其他语句不起作用。只在其他声明
- 2. 如果语句不似乎在检查其他&&在声明中
- 3. .equal不能在其他语句中工作如果语句为
- 4. 如果其他语句不工作
- 5. JS如果语句不与其他语句工作
- 6. 其他语句不工作在JavaScript(只返回如果和如果其他)
- 7. 如果声明与其他多个if语句在ssrs
- 8. 如果Python中的其他语句
- 9. 如果 - 其他语句不起作用
- 10. 如果声明不工作,因为我期望他们在python
- 11. 如果和其他语句不工作在jquery
- 12. 如果其他语句不工作在php
- 13. 如果在MDX语句中声明
- 14. 分类法PHP其他如果声明不工作?
- 15. 如果其他语句在crontab中工作错误?
- 16. 如果声明,不工作
- 17. 如果语句不工作(Python)的
- 18. 虽然循环如果其他语句不能正常工作
- 19. if/else语句不工作,并说其他需要如果
- 20. 我的jQuery如果其他语句不工作
- 21. 我的其他如果语句使用字符串不工作
- 22. 批处理如果其他语句不工作
- 23. 如果其他语句不按预期工作
- 24. 如果其他语句不按预期工作
- 25. 如果其他语句不能正常工作
- 26. PHP如果其他语句不能正常工作
- 27. 提交按钮不工作 - 如果其他语句
- 28. 如果其他OR语句在Assembly中?
- 29. 如果其他语句在Swift中
- 30. mysql如果其他语句
那是什么'elif'是 – Pynchia
相关:http://stackoverflow.com/questions/12774279/python-how-to-check- if-a-variable-is-equal-to-one-string-or-another-string –
因为你没有在范围(...)中使用'i'的值,所以我想建议使用'for _ in range(...)'。使用下划线作为循环变量是一种常见惯例,它强调某些事情正在重复,并且循环变量不重要。 – alexwlchan