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

时间:2016-06-26 11:17:21

标签: python random attributes restart

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

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

当用户按下&#34; q&#34;并要求程序绘制另一个形状,我希望程序回到最外面的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,但我得到了类似的错误。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

您依靠import random random = random.choice('123456') print (random) random = random.choice('123456') print(random) 成为random模块,该模块具有函数random。第一次调用它时,您将其结果(choice中的一个字符)分配给名称'123456',从而使名称random不再指向random模块。请改用其他名称,例如random

答案 1 :(得分:0)

首次启动程序时,可以将random变量设置为random.choice(),因为在调用random之前未创建变量random.choice()。但是,在seconde时间,您的变量random是一个字符串,当您尝试使用random更改random.choice的值时,它无法正常工作,因为python认为您正在尝试调用{您choice()变量上的{1}}并且您没有使用“随机库”。

所以你只需要用其他东西重命名你的变种random

例如,这将起作用:

random

但不是那样:

import random
random1 = random.choice('123456')
print (random1)
random1 = random.choice('123456')
print(random1)
相关问题