python - 花蟒——制作更多的花
问题描述
所以我正在制作这个程序,其中花朵刚刚出现,你设置了花瓣的数量。它还有另外两个输出,即是另一朵不同颜色的花,以及一颗用颜色填充自身的星星。我被这段代码困住了,因为我试图让多朵花出现在第一个输出中,但是如果笔不重叠绘制的第一朵花,我就无法得到它。
#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)
推荐阅读
- spring - Spring拦截器获取真实的requestURI
- python - 烧瓶路由中的多个“路径”参数导致解析不正确
- php - 带解构的 PHP 中的选择排序
- java - 带有opendjdk 8的eclipse ide的MAC OS Big Sur问题
- c# - 如何修复在处理按钮单击事件时似乎未运行的动画?
- c++ - 为什么在 C++ 中 true && false 等于 1?
- css - 我正在使用反应引导
标签。我想在大型设备上将其宽度修复为默认 1140 像素,但它超出了 1296 像素的范围 - memory-management - 如何从 Linux 上的 CMA(连续内存分配器)获得对齐的内存?
- tensorflow - 如何处理 TensorFlow 中的分类标签?
- c++ - 消除函数调用包装器