Python随机导致加载时冻结

时间:2018-10-20 19:14:11

标签: python-2.7 django-views

我正在尝试为自己创建一个小锻炼程序,以便在家中与Django一起使用。我有5个列表,其中包含上半身锻炼,下半身锻炼,全身锻炼,腹部锻炼,然后另一个包含几秒钟的随机时间。我提交的表格只是我要锻炼的分钟数。我正在使用python random首先通过while循环将时间列表随机化:

timelimits = [
    15,
    30,
    45,
    60,
    75,
    90
]


# This is inside of my views, rendering to index
time = form.cleaned_data['duration']
            workouttime = 0
            randomtimes = []
            while workouttime <= int(time) * 60:
                randomtime = random.choice(timelimits)
                randomtimes.append(randomtime)
                workouttime = workouttime + randomtime

,然后我建立了一个随机时间列表,但是有很多随机时间接近我在表单中指定的持续时间,我认为使用每个列表中的随机运动来建立锻炼:

exercises = 0
            workout = []
            station = 'a'
            while exercises < len(randomtimes):
                if station == 'a':
                    if len(lowerbody) > 0:
                        randomleg = random.choice(lowerbody)
                        lowerbody.remove(randomleg)
                        workout.append([randomleg, randomtimes[exercises]])
                        exercises = exercises + 1
                        print(exercises)
                    station = 'b'
                elif station == 'b':
                    if len(fullbody) > 0:
                        randomfb = random.choice(fullbody)
                        fullbody.remove(randomfb)
                        workout.append([randomfb, randomtimes[exercises]])
                        exercises = exercises + 1
                        print(exercises)
                    station = 'c'
                elif station == 'c':
                    if len(upperbody) > 0:
                        randomup = random.choice(upperbody)
                        upperbody.remove(randomup)
                        workout.append([randomup, randomtimes[exercises]])
                        exercises = exercises + 1
                        print(exercises)
                    station = 'd'
                elif station == 'd':
                    if len(abbs) > 0:
                        randomabbs = random.choice(abbs)
                        abbs.remove(randomabbs)
                        workout.append([randomabbs, randomtimes[exercises]])
                        exercises = exercises + 1
                        print(exercises)
                    station = 'a'

当我单击表单上的提交时,它开始打印到控制台,好像它可以正常工作,但是通常我的服务器只会旋转直到超时。如果我再提交一次表单,它将正确加载并为我建立一个具有随机关联时间的随机锻炼列表。无论是时间构建器还是锻炼构建器,它都冻结了,我真的不明白是什么使它冻结了。就像我说的那样,它不会起作用,然后,如果我重新提交,它将神奇地起作用。

0 个答案:

没有答案