gpt4 book ai didi

python - 完成后再次执行程序(Python)

转载 作者:太空宇宙 更新时间:2023-11-03 16:25:22 26 4
gpt4 key购买 nike

所以我正在将这一小段代码编写为一个小项目:它使用海龟图形模块来绘制各种形状。它通过随机模块选择要绘制的形状。这是代码。

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()

当用户按“q”并要求程序绘制另一个形状时,我希望程序返回到最外层 while 循环的顶部。我使用break来做到这一点并且效果很好。然而,当我遇到麻烦时:

random = random.choice('123456')

程序给我一个错误,说:

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'

我该如何解决这个问题?或者有没有办法重新启动整个程序?我也尝试过使用 random.randint 但出现类似的错误。提前致谢。

最佳答案

您依赖于 random 作为 random 模块,该模块具有函数 choice。第一次调用它时,将其结果('123456' 中的一个字符)分配给名称 random,从而使名称 random code> 不再指向 random 模块。请使用不同的名称,例如 random_number = random.choice('123456')

关于python - 完成后再次执行程序(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38037957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com