我的教授让我使用列表理解生成用户定义数量的龟对象,我无法弄清楚如何做到这一点。
import math
import turtle
import random
wn = turtle.Screen()
wn.setworldcoordinates(-300,-400,300,400)
wn.bgcolor('white')
num_of_turts = int(input("Enter the number of turtles to use: "))
turts = [turtle.Turtle() for t in range(num_of_turts)]:
wn.exitonclick()
这是一个开始,但由于'turtle.Turtle()'而导致语法错误无效
有什么想法吗?
规格:
1. Create a display area with coordinates in the range of a rectangle bounded by opposite diagonal corners at (-300, -400) and (300, 400).
2. From user input, determine a number n of turtles to create.
3. Use a list comprehension to create a list of n turtles
4. With and for the bale of turtles:
a. Begin each turtle moving randomly within the display area.
b. The turtles may be colored and color other than red or green.
c. There must be a function to calculate a turtle’s next location.
d. All turtles must remain in the display area.
5. Create a red turtle and make its position match the average location of the other n turtles.
a. There must be a function to calculate and return the average location of the n turtles.
b. The red turtle must leave a pen trail
6. Create a green turtle and make its position track exactly one of the current leftmost, rightmost, uppermost or bottommost turtle in the display.
a. There must be a function to calculate and return the proper coordinate of the current, extreme-most turtle.
b. The green turtle must leave a pen trail.
c. Use the green turtle to draw a line the complete width or height of the display area at each extreme value of turtle location found for the bale. Use a horizontal line if tracking upper- or lower-most. Use a vertical line, if tracking left- or rightmost
答案 0 :(得分:1)
你在行尾有一个冒号:
turts = [turtle.Turtle() for t in range(num_of_turts)]:
这在Python中无效。它应该是:
turts = [turtle.Turtle() for t in range(num_of_turts)]
除此之外,列表理解的语法还可以。