【问题标题】:Python list issue with simulator模拟器的Python列表问题
【发布时间】:2020-05-29 13:30:17
【问题描述】:

嘿,我正在尝试为学校制作一个强力球模拟器游戏,但由于某种原因,最终的 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)

【问题讨论】:

  • 什么是form?请提供Minimal, Reproducible Example
  • @Feodoran 抱歉,表单是我从中提取数据的地方,因此您将其输入为 html 表单,然后我使用该数据。
  • 抱歉,我的问题表述得不好。 form 持有什么数据?您应该为最小的、可重现的示例提供一些示例数据。
  • @Feodoran 哦,对不起,数据只是整数,实际上可以是任何东西,例如 nwb = 5 nwb2 = 5 rwb = 10 npb = 2 npb2 = 2 rpb = 20 ballswin = 0 pbwin = 0
  • 好的,现在到底是什么问题?很难猜测您的代码应该做什么。一些注释会很有帮助,或者至少是有意义的变量名。 “最终的 pbsim 值未打印”最终值是多少? “我希望 pbsim 和 wb 列表不断变化”如何更改?

标签: python list simulator


【解决方案1】:

有些变量为零,这就是它进入无限循环的原因。我已经改变了它。变化在变化。现在它的显示输出。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多