from turtle import*
def square():
for i in range(4):
forward(30)
right(90)
def triangle():
for i in range(3):
forward(50)
left(120)
def pentagon():
for i in range(5):
forward(30)
right(72)
def hexagon():
for i in range(6):
forward(30)
right(60)
def star():
for i in range(5):
forward(50)
right(144)
def pause():
penup()
forward(70)
pendown()
shape = (input("Type one of these shapes square, triangle, pentagon, hexagon, star"))
if shape == square():
print (square())
elif shape == triangle():
print (triangle())
elif shape == pentagon():
print (pentagon())
elif shape == hexagon():
print (hexagon())
elif shape == star():
print (star())
else:
print("Shape is not valid, please input a valid one!")
Tags:
当你写作时:
它调用
square
函数来绘制正方形。然后将用户的输入与返回值进行比较。由于函数不返回任何内容,因此比较失败你对所有的形状都这样做,所以它最终绘制了所有的形状
您应该将用户的输入与字符串进行比较,而不是调用函数
您也不应该在调用shape函数时使用
print()
,因为它们不会返回任何应该打印的内容。所以它应该是这样的:与所有的
if
语句不同,更聪明的方法是使用从形状名称映射到函数的字典:相关问题 更多 >
编程相关推荐