所以我正在制作这个程序,其中花朵刚刚出现,然后你设置花瓣的数量。它还有另外两个输出,即另一朵不同颜色的花,以及一颗充满颜色的星星。我被这段代码困住了,因为我试图让多朵花出现在第一个输出中,但如果笔不与绘制的第一朵花重叠,我就无法得到它。
#Code Notes: You want three outputs. The flower (not filled), a filled
flower with 6 different colors, and a spiraled star
#importing sys and turtle.
import sys
import turtle
#function for flower shape
def draw_square(square):
for i in range(0,2):
square.forward(100)
square.right(70)
square.forward(100)
square.right(110)
#function to draw the flower
def draw_flower(petalNumber):
window = turtle.Screen()
window.bgcolor("beige")
window.title("Flower")
#flower turtle
flower = turtle.Turtle()
flower.shape("triangle")
flower.color("teal")
flower.pencolor("beige")
flower.pensize(3)
flower.goto(15,70)
flower = "teal"
for number in range(0, petalNumber):
flower.begin_fill()
draw_square(flower)
flower.end_fill()
flower.right(360.0/petalNumber)
window.exitonclick() #when clicked the window closes
#function for colorful art (the petals are filled in with different colors)
def colorfulArt(numberOfPetals):
colors = ["red","blue","green","orange","pink","teal","purple","yellow"]
window = turtle.Screen()
window.bgcolor("white")
window.title("Pinwheel Flower")
flower = turtle.Turtle()
flower.shape("triangle")
flower.color("blue")
for petalNumber in range(numberOfPetals):
flower.begin_fill()
flower.color("white",colors[petalNumber%8])
draw_square(flower)
flower.end_fill()
flower.right(360.0/numberOfPetals)
window.exitonclick() #when clicked the window closes
#function for original art(which is just stars)
def originalStars(n):
window = turtle.Screen()
window.bgcolor("white")
window.title("Star Power")
star_spiral = turtle.Turtle()
star_spiral.begin_fill()
star_spiral.shape("triangle")
star_spiral.color("teal")
for count in range(20+n):
star_spiral.forward(count*15)
star_spiral.right(144)
star_spiral.end_fill()
window.exitonclick()
#let the user choose
print("Choose from the following:")
print("1. Flowers.")
print("2. Colorful Flower.")
print("3. Spiraled Star.")
UserChoice = int(input())
#if statement check the user choice
if (UserChoice == 1):
numberOfPetals= int(input("How many petals?"))
#the next line calls the draw flower function
draw_flower(numberOfPetals)
#elif- chapter 5 conditional statement
elif(UserChoice == 2):
numberOfPetals = int(input("How many petals?"))
colorfulArt(numberOfPetals) #this line calls the colorful art function
else:
(UserChoice == 3)
numberOfPetals= int(input("How many stars should overlap?"))
originalStars(numberOfPetals) #this line calls the original stars
function
我注意到的一个问题是你的陈述
flower="teal"
。通过这样做,您已将 flower
对象(变量)转换为字符串而不是海龟对象,因此此后的其余代码将失败。
下面的代码对我有用:
import sys
import turtle
def draw_square(square):
for i in range(0, 2):
square.forward(100)
square.right(70)
square.forward(100)
square.right(110)
def draw_flower(petal_number: int):
"""
Draws a flower on the screen with a given number of petals
:param petal_number:
:return: Blocks until the user clicks on the screen
"""
window = turtle.Screen()
window.bgcolor("beige")
window.title("Flower")
flower = turtle.Turtle()
flower.shape("triangle")
flower.color("teal")
flower.pencolor("beige")
flower.pensize(3)
flower.goto(15,70)
for number in range(0, petal_number):
flower.begin_fill()
draw_square(flower)
flower.end_fill()
flower.right(360.0 / petal_number)
window.exitonclick()
if __name__ == "__main__":
draw_flower(3)
最好的花店在木尔坦。