为Powerball模拟器生成5个唯一数字

2024-10-01 00:36:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我是从课堂上提供的伪代码中写出来的,我已经弄明白了其中的大部分。我遇到的唯一问题是它返回前5个“球”的重复数字,我根本不知道为什么。伪代码中我不确定的一行是:“如果该数字不在主数字序列中”。我是这样编码的:

if number != mainNumbers:

这可能是个问题,但我不知道如何编写代码

from random import *

def drawing():
    balls=0
    mainNumbers=[]
    
    while balls < 5:
        number=randint(1,69)
        if number != mainNumbers:
            mainNumbers.append(number)
            balls = balls + 1

    mainNumbers.sort()
    pBall=randint(1,26)
    return mainNumbers, pBall

def main():
    print("This program simulates a user defined number of Powerball drawings\n")
    runs = int(input("What's the total number of drawings? "))
    print()
    count = 1
    while count <= runs:
        balls, pBall = drawing()
        print("Drawing: {0} - The numbers are: {1} and the Powerball is: {2}".format(count, balls, pBall))
        count = count + 1
main()

Tags: of代码numberifmaindefcount数字