【问题标题】:Python, user input generates turtlesPython,用户输入生成海龟
【发布时间】:2016-12-05 14:05:39
【问题描述】:

我正在尝试设置一个程序,在该程序中,用户决定生成多少海龟,然后他们进行比赛。我目前的解决方案是从用户那里获取一个 int 输入并执行下面的代码(代码不断重复较大的数字)。我尝试过循环,但我遇到了麻烦,因为它们最终都需要执行随机动作。有什么帮助吗?

if turtNum >= 1:   
  turt1 = Turtle()
  turt1.color(turtColour[0])
  turt1.shape('turtle')

  turt1.penup()
  turt1.goto(0, -10)
  turt1.pendown()

  if turtNum >= 2:

    turt2Name = input('Enter a name for the second turtle: ')

    turt2 = Turtle()
    turt2.color(turtColour[1])
    turt2.shape('turtle')

    turt2.penup()
    turt2.goto(0, -25)
    turt2.pendown()

这是我尝试过的代码,但出现此错误“列表索引必须是整数或切片,而不是 str”

turtName = []
maxLengthList = turtNum
while len(turtName) < maxLengthList:
    name = input('Enter the names for the turtles: ')
    turtName.append(name)

for i in turtName:
    turtName[i] = Turtle()
    turtName[i].color(turtColour[0])
    turtName[i].shape('turtle')

    turtName[i].penup()
    turtName[i].goto(0, -10)
    turtName[i].pendown()

【问题讨论】:

  • 有什么问题?
  • 如果你想你使用随机位置(坐标去)你可以使用random.random(cordinate_min ,cordinate_max)得到随机cordiante
  • @ChrisMueller 好吧,我正在尝试在一个循环中生成所有海龟。我希望用户输入一个整数,而不是当前写出我的代码的方式,它会绘制那么多海龟
  • @user7252321 然后使用input() 获取数字并将文本数字转换为int() - number = int(text_with_number)number = int(input(...))
  • 您可以考虑在类构造函数中调用.shape.color.penup.goto.pendown,这样会更好地遵守 DRY 并使其成为从概念上解决更简单的问题...

标签: python turtle-graphics


【解决方案1】:

你不能不期待我们看到它的发生而兴奋地提出海龟赛跑的概念。下面是一个粗略的实现,解决了您在输入海龟数量、输入单个海龟信息、全部存储和随机运动方面遇到的问题:

from turtle import Turtle, Screen
from itertools import cycle
from random import randrange

MAX_TURTLES = 20
LANE_WIDTH = 25
FONT_SIZE = 18
FONT = ("Arial", FONT_SIZE, "normal")
COLORS = cycle(['red', 'green', 'blue', 'cyan', 'black', 'yellow'])
FINISH_LINE = 350
START_LINE = -200
NAME_LINE = START_LINE - 150
DELAY = 100  # milliseconds
MAX_STEP = 10

turtles = dict()

def race():
    for name, turtle in turtles.items():  # should shuffle turtles
        turtle.forward(randrange(MAX_STEP + 1))

        if turtle.xcor() > FINISH_LINE:
            return  # the race is over

    screen.ontimer(race, DELAY)

magic_marker = Turtle(visible=False)
magic_marker.penup()

turtNum = 0

while not 1 <= turtNum <= MAX_TURTLES:
    turtNum = int(input('Enter the number of turtles: '))

for i in range(turtNum):

    name = input('Enter a name for the turtle #{}: '.format(i + 1))

    turtle = Turtle(shape="turtle")
    turtle.color(next(COLORS))

    y_offset = LANE_WIDTH * i - LANE_WIDTH * turtNum // 2

    magic_marker.color(turtle.pencolor())
    magic_marker.goto(NAME_LINE, y_offset - FONT_SIZE / 2)
    magic_marker.write(name, font=FONT)

    turtle.penup()
    turtle.goto(START_LINE, y_offset)
    turtle.pendown()

    turtles[name] = turtle

magic_marker.color('red')
magic_marker.goto(FINISH_LINE, -FINISH_LINE)
magic_marker.pendown()
magic_marker.goto(FINISH_LINE, FINISH_LINE)
magic_marker.penup()

screen = Screen()

screen.ontimer(race, DELAY)

screen.exitonclick()

【讨论】:

    猜你喜欢
    • 2015-05-18
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多