首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模拟器的Python列表问题

模拟器的Python列表问题
EN

Stack Overflow用户
提问于 2020-05-29 21:30:17
回答 1查看 69关注 0票数 0

嘿,我正在尝试为学校制作一个powerball模拟游戏,但由于某种原因,最终的pbsim值没有打印出来。此外,我希望pbsim和wb列表在while循环中不断变化,如果有人能想出如何做到这一点,将非常感谢:)谢谢

这段代码是为了我的学校作业,我正在尝试制作一个powerball模拟器,所以我希望人们能够将值输入到表单中,这段代码将运行并返回代码必须运行的次数,直到获得胜利!

wbpbsim应该在每次循环时都是随机的,但目前并非如此。

python代码

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-06-04 21:10:56

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62087143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档