编写一个程序,要求用户提供颜色,线宽,线长和形状。形状应为直线,三角形或正方形。使用乌龟图形绘制用户要求的形状,包括用户要求的大小,颜色,线宽和线长。例如,如果这些是用户选择的颜色,宽度,线长和形状。
输出应如下所示:什么颜色?蓝色什么线宽?25什么线长?100条线,三角形或正方形?三角形
我的代码:
import turtle
colour=input('What color? ')
width=int(input('What width? '))
length=int(input('What line length? '))
shape=input('line, triangle, or square? ')
if shape=='line' or 'Line' or 'LINE':
t=turtle.Turtle()
t.pendown()
t.color(colour)
t.pensize(width)
t.forward(length)
elif shape=='triangle' or 'Triangle' or 'TRIANGLE':
t=turtle.Turtle()
t.pendown()
t.color(colour)
t.pensize(width)
t.forward(length)
t.left(120)
t.forward(length)
t.left(120)
t.forward(length)
elif shape=='square' or 'Square' or 'SQUARE':
t=turtle.Turtle()
t.pendown()
t.color(colour)
t.pensize(width)
t.forward(length)
t.left(90)
t.forward(length)
t.left(90)
t.forward(length)
t.left(90)
t.forward(length)
else:
print('Command not recognized, try again!')
另外,我的输出仅会持续到第一个“ if”语句,之后不会继续执行。它接受用户提出的前三个问题,但是无论第四个问题的答案是什么,它始终是一行。
shape=='line' or 'Line' or 'LINE'
转换为:
shape=='line' or True or True
因为对象是真实的,可以通过输入以下内容在REPL上轻松对其进行测试:
if 'line': print 'true'
shape=='line' or shape=='Line' or shape=='LINE'
if re.match('line', shape.strip(), re.IGNORECASE):
if
在这种情况下,您甚至不需要任何声明!
def line():
print 'Line selected!'
def square():
print 'Square selected'
def triangle():
print 'Triangle selected'
globals()[shape.strip().lower()]()
getattr(module,method_name);
警告:少量使用可能会很优雅,如果使用过多会变得完全难以维护
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句