模拟器的Python列表问题

问题描述 投票:0回答:1

嘿,我正在尝试为学校制作强力球模拟器游戏,由于某种原因,最终的pbsim值未打印。另外,我希望pbsim和wb列表在while循环内不断变化,如果有人能弄清楚该怎么做,将不胜感激:)谢谢

该代码用于我的学校作业,我试图制作一个强力球模拟器,所以我希望人们能够将值输入到表单中,此代码将运行并返回该代码必须执行的次数。直到赢得胜利为止!

[wbpbsim]每次经过循环时都应该始终是随机的,但当前不是。另外,我不希望在同一列表中重复任何值。

因此,总而言之,我需要pbsim和wb是随机的,并在每次while循环== false处于活动状态时进行更改

python代码

import random
nwb = 5     # number of winning balls
rwb = 10    # range of winning balls
npb = 2     # number of powerballs
rpb = 20    # range of powerballs

# randomly draw a list of winning balls
actual = [] 
while nwb > 0:
    x = random.randint(1, rwb)
    if x not in actual:
        actual.append(x)
        nwb = nwb - 1
        actual.sort()

# randomly draw a list of winning powerballs
pbactual = []   
while npb > 0:
    x = (random.randint(1, rpb))
    if x not in pbactual:
        pbactual.append(x)
        npb = npb - 1
        pbactual.sort()        


win = False
run = True
count = 0
nwb2 = nwb
npb2 = npb
wb = []     # list of randomly choosen balls 
pbsim = []  # list of randomly choosen powerballs 

while win == False:
    while run:
        x = random.randint(1, rwb)
        if x not in wb:
            wb.append(x)
            pbactual.sort()

        if len(wb) == nwb2:
            while npb2 > 0:
                x = random.randint(1, rpb)
                if x not in pbsim:
                    pbsim.append(x)
                    npb2 = npb2 - 1
            run = False



    actual.sort()
    wb.sort()
    pbactual.sort()
    pbsim.sort()
    count += 1
    print(actual, wb, pbactual, pbsim)
python list simulator
1个回答
0
投票

一些变量为零,这就是为什么它陷入无限循环。我已经改变了。更改在changes中。现在显示输出。

import random
nwb = 5     # number of winning balls
rwb = 10    # range of winning balls
npb = 2     # number of powerballs
rpb = 20    # range of powerballs

# randomly draw a list of winning balls
actual = []
while nwb > 0:
    x = random.randint(1, rwb)
    if x not in actual:
        actual.append(x)
        nwb = nwb - 1
        actual.sort()

# randomly draw a list of winning powerballs
pbactual = []
while npb > 0:
    x = (random.randint(1, rpb))
    if x not in pbactual:
        pbactual.append(x)
        npb = npb - 1
        pbactual.sort()


win = False
run = True
count = 0
**nwb2 = len(actual)
npb2 = len(pbactual)**
wb = []     # list of randomly choosen balls
pbsim = []  # list of randomly choosen powerballs

while win == False:
    while run:
        x = random.randint(1, rwb)
        if x not in wb:
            wb.append(x)
            pbactual.sort()

        if len(wb) == nwb2:
            while npb2 > 0:
                x = random.randint(1, rpb)
                if x not in pbsim:
                    pbsim.append(x)
                    npb2 = npb2 - 1
            run = False
            **win = True**



    actual.sort()
    wb.sort()
    pbactual.sort()
    pbsim.sort()
    count += 1
    print(actual, wb, pbactual, pbsim)
© www.soinside.com 2019 - 2024. All rights reserved.