So I'm writing this little piece of code as a small project: It uses the turtle graphics module to draw various shapes. It picks which shape to draw by way of the random module. Here's the code.
import time
import sys
import random
import os
from turtle import *
while True:
value = 0
def f():
global value
value = 1
onkey(f, "q")
listen()
random = random.choice('123456')
print(random)
if random == "1":
#Star
from turtle import *
hideturtle()
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
textinput("again1", "Again?")
if textinput == "Yes"or"yes"or"Y"or"y":
clearscreen()
break
if value == 1:
textinput("again1", "Again?")
if textinput == "Yes"or"yes"or"Y"or"y":
clearscreen()
break
end_fill()
done()
elif random == "2":
#Extending Squares
from turtle import *
hideturtle()
color('red', 'yellow')
size=1
begin_fill()
while (True):
forward(size)
right(91)
size = size + 1
if value == 1:
textinput("again2", "Again?")
if textinput == "Yes"or"yes"or"Y"or"y":
clearscreen()
break
elif random == "3":
#Extending Hexagons
from turtle import *
hideturtle()
color('red','yellow')
size=1
while True:
forward(size)
right(61)
size = size + 1
if value == 1:
textinput("again3", "Again?")
if textinput == "Yes"or"yes"or"Y"or"y":
clearscreen()
break
elif random == "4":
#Recursive Star
import turtle
from turtle import *
def star(turtle, n,r):
""" draw a star of n rays of length r"""
for k in range(0,n):
turtle.pendown()
turtle.forward(r)
turtle.penup()
turtle.backward(r)
turtle.left(360/n)
def recursive_star(turtle, n, r, depth, f):
"""At each point of the star, draw another smaller star,
and so on, up to given depth. Rescale the stars by a factor f
at each generation."""
if depth == 0:
star(turtle, n, f*4)
else:
for k in range(0,n):
turtle.pendown()
turtle.forward(r)
recursive_star(turtle, n, f*r, depth - 1,f)
turtle.penup()
turtle.backward(r)
turtle.left(360/n)
if value == 1:
textinput("again4", "Again?")
if textinput == "Yes"or"yes"or"Y"or"y":
clearscreen()
break
fred = turtle.Turtle()
fred.speed("fastest")
fred.color('red','yellow')
fred.hideturtle()
recursive_star(fred, 5 , 150, 4, 0.4)
elif random == "5":
#Honeycombs
from turtle import *
color('red','yellow')
penup()
setx(-200)
sety(-150)
pendown()
side = 0
side1 = 0
begin_fill()
while True:
forward(50)
right(60)
side = side + 1
side1 = side1 + 1
if value == 1:
textinput("again5", "Again?")
if textinput == "Yes"or"yes"or"Y"or"y":
clearscreen()
break
if side == 6:
side = 0
end_fill()
begin_fill()
while True:
forward(50)
left(60)
side = side + 1
side1 = side1 + 1
if value == 1:
break
if side == 6:
end_fill()
side = 0
forward(50)
left(60)
begin_fill()
break
if side1 == 72:
side1 = 0
forward(50)
left(60)
forward(50)
right(60)
elif random == "6":
#Lattice of Squares
color('red','yellow')
while True:
forward(200)
right(91)
forward(50)
right(91)
forward(50)
right(91)
if value == 1:
textinput("again6", "Again?")
if textinput == "Y"or"y"or"Yes"or"yes":
clearscreen()
break
else: sys.exit()
When the user presses "q" and asks the program to draw another shape, I want the program to go back to the top of the very outermost while loop. I'm using break to do this and it works fine. However, I run into trouble when it gets to:
random = random.choice('123456')
The program gives me an error, saying:
Traceback (most recent call last):
File "C:\Users\aerro_000\Desktop\PrettyShapes.py", line 14, in <module>
random = random.choice('123456')
AttributeError: 'str' object has no attribute 'choice'
How do I fix this? Or is there a way to just restart the entire program? I've also tried using random.randint but I get a similar error. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire