Python:编写一个程序,询问用户颜色,线宽,线长和形状

时间:2015-02-06 00:07:07

标签: python turtle-graphics

  

编写一个程序,询问用户颜色,线宽,线长和形状。形状应该是直线,三角形或正方形。使用乌龟图形绘制用户请求的形状,用户请求的大小,颜色,线宽和线长。例如,如果这些是用户对颜色,宽度,线长和形状的选择

什么颜色?蓝色
什么线宽? 25个
什么线长? 100个
线,三角形还是方形?三角形

这是我的尝试:

color = input('Enter your preferred turtle line color: ') 
width = input('Enter your preferred turtle line width: ')
length = input('Enter your preferred turtle line length: ')
shape = input('Specify whether you want to draw a line, triangle, or square: ')

import turtle
s = turtle.Screen()
t = turtle.Turtle()
t.pencolor(color)
t.pensize(width)
if shape == 'line':
    t.forward(length)

elif shape == 'triangle':
    t.forward(length)
    t.right(45)
    t.forward(length)
    t.right(90)
    t.forward(length)

else:
    t.forwad(length)
    t.right(90)
    t.forwad(length)
    t.right(90)
    t.forwad(length)
    t.right(90)
    t.forwad(length)

我收到此错误:

Traceback (most recent call last):
  File "<pyshell#42>", line 4, in <module>
    t.forward(length)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/turtle.py", line 1637, in forward
    self._go(distance)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/turtle.py", line 1604, in _go
    ende = self._position + self._orient * distance
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/turtle.py", line 257, in __mul__
    return Vec2D(self[0]*other, self[1]*other)
TypeError: can't multiply sequence by non-int of type 'float'

有人可以解释为什么前进命令中的长度不会通过吗?

5 个答案:

答案 0 :(得分:1)

我看到的第一个问题是你已经离开了'r'。

t.forwad(length)

应该是

t.forward(length)

此外,如果您使用input() width并且length将是字符串,但它们需要进行类型转换。 Specifically,长度必须是整数或浮点数,宽度必须是正整数。

length = None
while not length:
    try:
        length = float(input('Enter your preferred turtle line length: '))
    except ValueError:
        print('You need to enter a number')

width = None
while not width:
    try:
        width = int(input('Enter your preferred turtle line width: '))
    except ValueError:
        print('You need to enter a positive integer')
    else:
        if width < 1:
            print('You need to enter a positive integer')
            width = None

我这里的代码将使用循环来获取用户的正确输入。它会试图拒绝糟糕的输入。例如,如果用户在询问长度时键入“pumpkin”。

类似地,当我捕捉长度和宽度条目的问题时,你会想要捕获用户输入形状和颜色的问题。确保用户输入有效颜色。确保形状在允许的形状列表中。

最后一个问题是这里的代码没有正确缩进。您需要在if:else:条款后缩进。

这是整个计划的工作:

import turtle

s = turtle.Screen()
t = turtle.Turtle()

length = None
while not length:
    try:
        length = float(input('Enter your preferred turtle line length: '))
    except ValueError:
        print('You need to enter a number')

width = None
while not width:
    try:
        width = int(input('Enter your preferred turtle line width: '))
    except ValueError:
        print('You need to enter a positive integer')
    else:
        if width < 1:
            print('You need to enter a positive integer')
            width = None
color = None
while not color:
    color = input('Enter your preferred turtle line color: ')
    try:
        t.pencolor(color)
    except:
        print('You need to enter a color that I know.')
        color = None
shape = None
while not shape:
    shape = input('Specify whether you want to draw a line, triangle, or square: ')
    if shape.lower() not in ['line', 'triangle', 'square']:
        shape = None
        print('I only draw lines, triangles and squares!')


t.pensize(width)
if shape.lower() == 'line':
    t.forward(length)
elif shape.lower() == 'triangle':
    t.forward(length)
    t.right(120)
    t.forward(length)
    t.right(120)
    t.forward(length)
else:
    t.forward(length)
    t.right(90)
    t.forward(length)
    t.right(90)
    t.forward(length)
    t.right(90)
    t.forward(length)

s.exitonclick()

请注意,我还修复了三角形...

答案 1 :(得分:0)

如果您想要的值可以使用turtle模块提供的图形输入方法获得,那么将命令行输入与乌龟图形混合似乎是一个坏主意:

  • textinput(title, prompt)

  • numinput(title, prompt, default=None, minval=None, maxval=None)

这些旨在防止命令行代码需要捕获的一些错误。这是用图形输入法重写的程序:

from turtle import Turtle, Screen, TurtleGraphicsError

screen = Screen()

length = None
while not length:
    length = screen.numinput('Select Length', 'Enter your desired line length:', minval=1)

width = None
while not width:
     width = screen.numinput('Select Width', 'Enter your desired line width:', minval=1, default=1)

turtle = Turtle()
turtle.pensize(width)

color = None
while not color:
    try:
        color = screen.textinput('Select Color', 'Enter your desired line color:')
        turtle.pencolor(color)
    except TurtleGraphicsError:
         color = None

shape = None
while not shape or shape.lower() not in {'line', 'triangle', 'square'}:
    shape = screen.textinput('Select Shape', 'Specify whether you want a line, triangle, or square:')
shape = shape.lower()

if shape == 'line':
    turtle.forward(length)
elif shape == 'triangle':
    for _ in range(3):
        turtle.forward(length)
        turtle.right(120)
else:
    for _ in range(4):
        turtle.forward(length)
        turtle.right(90)

screen.exitonclick()

答案 2 :(得分:-1)

您的其他条件语法有错误:t.forwad(length)而不是t.forward(length)

答案 3 :(得分:-1)

您正在使用Python 3.x,input()返回一个字符串,而不是一个浮点数。 在将结果用作数字之前,您需要转换结果:

length = float(input('Enter your preferred line length: '))

问题是turtle类中的某个地方,length乘以浮点数。如果length本身就是一个数字,一切都会好的。但在您的情况下,length是一个字符串,因此Python必须执行string * float。现在,string被视为一系列字符。在Python中,将序列乘以整数('ab'*3 = 'ababab')是有效的,而不是其他类型(特别是浮点数)。这就是为什么你得到关于用非int类型乘以序列的错误消息。

答案 4 :(得分:-1)

import turtle

s = turtle.Screen()
t = turtle.Turtle()

length = None
while not length:
    try:
        length = float(input('Enter your preferred turtle line length: '))
    except ValueError:
        print('You need to enter a number')

width = None
while not width:
    try:
        width = int(input('Enter your preferred turtle line width: '))
    except ValueError:
        print('You need to enter a positive integer')
    else:
        if width < 1:
            print('You need to enter a positive integer')
            width = None
color = None
while not color:
    color = input('Enter your preferred turtle line color: ')
    try:
        t.pencolor(color)
    except:
        print('You need to enter a color that I know.')
        color = None
shape = None
while not shape:
    shape = input('Specify whether you want to draw a line, triangle, or square: ')
    if shape.lower() not in ['line', 'triangle', 'square']:
        shape = None
        print('I only draw lines, triangles and squares!')strong text