Python 我如何反复延迟随机选择?

Python 我如何反复延迟随机选择?,python,random,repeat,Python,Random,Repeat,ball_x=[100200300] 我需要知道如何对“过程”进行编程,使其在“0.5”秒后重复自身,然后再睡眠“0.5”秒,如此类推,永远重复这个过程 如果您想将进程放入一个0.5秒延迟的无止境循环中,您可以这样做 global bullet global ball global ball_x pred1 = [bullet, ball] pred2 = random.choice(pred1) process = image(pred2, ba

ball_x=[100200300]


我需要知道如何对“过程”进行编程,使其在“0.5”秒后重复自身,然后再睡眠“0.5”秒,如此类推,永远重复这个过程

如果您想将
进程
放入一个0.5秒延迟的无止境循环中,您可以这样做

    global bullet
    global ball
    global ball_x
    pred1 = [bullet, ball]
    pred2 = random.choice(pred1)
    process = image(pred2, ball_x, 10)
time.sleep(0.5)

首先添加
import time
from time import sleep
。然后在
中,当为True时:
循环执行
时间。睡眠(0.5)
您可以尝试下面的代码,它将在睡眠和投掷球之间交替,然后休息

while True:
    process = image(pred2, ball_x, 10)
    time.sleep(0.5)

谢谢我会:)
global bullet
global ball
global ball_x
pred1 = [bullet, ball]
throw = False
while true:
    pred2 = random.choice(pred1)
    if throw:
        time.sleep(0.5)
        throw = False
    else:
        process = image(pred2, ball_x, 10)
        time.sleep(0.5)
        throw = True