复杂的乐透号码生成器-Python

时间:2018-10-23 15:23:24

标签: python random

我已经老了,正在学习蟒蛇以使旧面条保持锋利,并非常喜欢它。我以前从未编码。

我的项目集中在生成乐透号码的乐趣上。选择5个不重复的随机数非常容易。

因此,接下来我想随机生成数字,直到从1到70的5个数字生成10次为止。一旦该号码产生了10次,那将是我的第一个乐透号码,依此类推。

请参阅我残酷的穴居人解决方案。它有效,我只是从打印列表中选择前5个。并从兆数列表中第一个。但我正在寻找“美化”或Pythony解决方案的提示。

多谢 SpookyWatcher

#My Lotto Numbers

import random

count = 0

lotteryNumbers = []
megaNumber = []

x1 = 0
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
x17 = 0
x18 = 0
x19 = 0
x20 = 0
x21 = 0
x22 = 0
x23 = 0
x24 = 0
x25 = 0
x26 = 0
x27 = 0
x28 = 0
x29 = 0
x30 = 0
x31 = 0
x32 = 0
x33 = 0
x34 = 0
x35 = 0
x36 = 0
x37 = 0
x38 = 0
x39 = 0
x40 = 0
x41 = 0
x42 = 0
x43 = 0
x44 = 0
x45 = 0
x46 = 0
x47 = 0
x48 = 0
x49 = 0
x50 = 0
x51 = 0
x52 = 0
x53 = 0
x54 = 0
x55 = 0
x56 = 0
x57 = 0
x58 = 0
x59 = 0
x60 = 0
x61 = 0
x62 = 0
x63 = 0
x64 = 0
x65 = 0
x66 = 0
x67 = 0
x68 = 0
x69 = 0
x70 = 0

y1 = 0
y2 = 0
y3 = 0
y4 = 0
y5 = 0
y6 = 0
y7 = 0
y8 = 0
y9 = 0
y10 = 0
y11 = 0
y12 = 0
y13 = 0
y14 = 0
y15 = 0
y16 = 0
y17 = 0
y18 = 0
y19 = 0
y20 = 0
y21 = 0
y22 = 0
y23 = 0
y24 = 0
y25 = 0

while count < 400:
    count += 1
    number = random.randint(1,70)
    #print (number)
    if number == 1:
        x1 += 1
    elif number == 2:
        x2 += 1
    elif number == 3:
        x3 += 1
    elif number == 4:
        x4 += 1
    elif number == 5:
        x5 += 1
    elif number == 6:
        x6 += 1
    elif number == 7:
        x7 += 1
    elif number == 8:
        x8 += 1
    elif number == 9:
        x9 += 1
    elif number == 10:
        x10 += 1
    elif number == 11:
        x11 += 1
    elif number == 12:
        x12 += 1
    elif number == 13:
        x13 += 1
    elif number == 14:
        x14 += 1
    elif number == 15:
        x15 += 1
    elif number == 16:
        x16 += 1
    elif number == 17:
        x17 += 1
    elif number == 18:
        x18 += 1
    elif number == 19:
        x19 += 1
    elif number == 20:
        x20 += 1
    elif number == 21:
        x21 += 1
    elif number == 22:
        x22 += 1
    elif number == 23:
        x23 += 1
    elif number == 24:
        x24 += 1
    elif number == 25:
        x25 += 1
    elif number == 26:
        x26 += 1
    elif number == 27:
        x27 += 1
    elif number == 28:
        x28 += 1
    elif number == 29:
        x29 += 1
    elif number == 30:
        x30 += 1
    elif number == 31:
        x31 += 1
    elif number == 32:
        x32 += 1
    elif number == 33:
        x33 += 1
    elif number == 34:
        x34 += 1
    elif number == 35:
        x35 += 1
    elif number == 36:
        x36 += 1
    elif number == 37:
        x37 += 1
    elif number == 38:
        x38 += 1
    elif number == 39:
        x39 += 1
    elif number == 40:
        x40 += 1
    elif number == 41:
        x41 += 1
    elif number == 42:
        x42 += 1
    elif number == 43:
        x43 += 1
    elif number == 44:
        x44 += 1
    elif number == 45:
        x45 += 1
    elif number == 46:
        x46 += 1
    elif number == 47:
        x47 += 1
    elif number == 48:
        x48 += 1
    elif number == 49:
        x49 += 1
    elif number == 50:
        x50 += 1
    elif number == 51:
        x51 += 1
    elif number == 52:
        x52 += 1
    elif number == 53:
        x53 += 1
    elif number == 54:
        x54 += 1
    elif number == 55:
        x55 += 1
    elif number == 56:
        x56 += 1
    elif number == 57:
        x57 += 1
    elif number == 58:
        x58 += 1
    elif number == 59:
        x59 += 1
    elif number == 60:
        x60 += 1
    elif number == 61:
        x61 += 1
    elif number == 62:
        x62 += 1
    elif number == 63:
        x63 += 1
    elif number == 64:
        x64 += 1
    elif number == 65:
        x65 += 1
    elif number == 66:
        x66 += 1
    elif number == 67:
        x67 += 1
    elif number == 68:
        x68 += 1
    elif number == 69:
        x69 += 1
    elif number == 70:
        x70 += 1
    if x1 == 10:
        lotteryNumbers.append(number)#print
    elif x2 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 2")
    elif x3 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 3")
    elif x4 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 4")
    elif x5 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 5")
    elif x6 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 6")
    elif x7 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 7")
    elif x8 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 8")
    elif x9 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 9")
    elif x10 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 10")
    elif x11 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 11")
    elif x12 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 12")
    elif x13 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 13")
    elif x14 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 14")
    elif x15 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 15")
    elif x16 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 16")
    elif x17 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 17")
    elif x18 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 18")
    elif x19 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 19")
    elif x20 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 20")
    elif x21 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 21")
    elif x22 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 22")
    elif x23 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 23")
    elif x24 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 24")
    elif x25 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 25")
    elif x26 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 26")
    elif x27 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 27")
    elif x28 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 28")
    elif x29 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 29")
    elif x30 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 30")
    elif x31 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 31")
    elif x32 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 32")
    elif x33 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 33")
    elif x34 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 34")
    elif x35 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 35")
    elif x36 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 36")
    elif x37 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 37")
    elif x38 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 38")
    elif x39 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 39")
    elif x40 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 40")
    elif x41 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 41")
    elif x42 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 42")
    elif x43 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 43")
    elif x44 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 44")
    elif x45 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 45")
    elif x46 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 46")
    elif x47 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 47")
    elif x48 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 48")
    elif x49 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 49")
    elif x50 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 50")
    elif x51 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 51")
    elif x52 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 52")
    elif x53 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 53")
    elif x54 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 54")
    elif x55 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 55")
    elif x56 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 56")
    elif x57 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 57")
    elif x58 == 10:
        lotteryNumbers.append(number)# print ("Your 1st Lucky number = 58")
    elif x59 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 59")
    elif x60 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 60")
    elif x61 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 61")
    elif x62 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 62")
    elif x63 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 63")
    elif x64 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 64")
    elif x65 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 65")
    elif x66 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 66")
    elif x67 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 67")
    elif x68 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 68")
    elif x69 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 69")
    elif x70 == 10:
        lotteryNumbers.append(number)#print ("Your 1st Lucky number = 70")
    meganumber = random.randint(1,70)
    #print(meganumber)
    #print (number)
    if meganumber == 1:
        y1 += 1
    elif meganumber == 2:
        y2 += 1
    elif meganumber == 3:
        y3 += 1
    elif meganumber == 4:
        y4 += 1
    elif meganumber == 5:
        y5 += 1
    elif meganumber == 6:
        y6 += 1
    elif meganumber == 7:
        y7 += 1
    elif meganumber == 8:
        y8 += 1
    elif meganumber == 9:
        y9 += 1
    elif meganumber == 10:
        y10 += 1
    elif meganumber == 11:
        y11 += 1
    elif meganumber == 12:
        y12 += 1
    elif meganumber == 13:
        y13 += 1
    elif meganumber == 14:
        y14 += 1
    elif meganumber == 15:
        y15 += 1
    elif meganumber == 16:
        y16 += 1
    elif meganumber == 17:
        y17 += 1
    elif meganumber == 18:
        y18 += 1
    elif meganumber == 19:
        y19 += 1
    elif meganumber == 20:
        y20 += 1
    elif meganumber == 21:
        y21 += 1
    elif meganumber == 22:
        y22 += 1
    elif meganumber == 23:
        y23 += 1
    elif meganumber == 24:
        y24 += 1
    elif meganumber == 25:
        y25 += 1
    if y1 == 10:
        megaNumber.append(meganumber)
    elif y2 == 10:
        megaNumber.append(meganumber)
    elif y3 == 10:
        megaNumber.append(meganumber)
    elif y4 == 10:
        megaNumber.append(meganumber)
    elif y5 == 10:
        megaNumber.append(meganumber)
    elif y6 == 10:
        megaNumber.append(meganumber)
    elif y7 == 10:
        megaNumber.append(meganumber)
    elif y8 == 10:
        megaNumber.append(meganumber)
    elif y9 == 10:
        megaNumber.append(meganumber)
    elif y10 == 10:
        megaNumber.append(meganumber)
    elif y11 == 10:
        megaNumber.append(meganumber)
    elif y12 == 10:
        megaNumber.append(meganumber)
    elif y13 == 10:
        megaNumber.append(meganumber)
    elif y14 == 10:
        megaNumber.append(meganumber)
    elif y15 == 10:
        megaNumber.append(meganumber)
    elif y16 == 10:
        megaNumber.append(meganumber)
    elif y17 == 10:
        megaNumber.append(meganumber)
    elif y18 == 10:
        megaNumber.append(meganumber)
    elif y19 == 10:
        megaNumber.append(meganumber)
    elif y20 == 10:
        megaNumber.append(meganumber)
    elif y21 == 10:
        megaNumber.append(meganumber)
    elif y22 == 10:
        megaNumber.append(meganumber)
    elif y23 == 10:
        megaNumber.append(meganumber)
    elif y24 == 10:
        megaNumber.append(meganumber)
    elif y25 == 10:
        megaNumber.append(meganumber)
print("Today's Lottery Numbers Are: ")
print(lotteryNumbers)
print("Today's MegaNumber is: ")
print(megaNumber)

全部感谢您的大力帮助。尤其是那些给出指示而无解的人。这是我在短短1个小时内想到的。

#My Lotto Numbers

import random

lottoNumbers = []
lottoMega = []
lottoNumberscondensed = []
numbersX = [0] * 71
numbersY = [0] * 26

for count in range(800):
    number = random.randint(1,70)
    numbersX[number] += 1
    if numbersX[number] == 10:
        lottoNumbers.append(number)
    number2 = random.randint(1,25)
    numbersY[number2] += 1
    if numbersY[number2] == 10:
        lottoMega.append(number2)
for item1 in lottoNumbers[0:5]:
    lottoNumberscondensed.append(item1)
lottoNumberscondensed.sort()
print("Todays Lotto Numers are: ")
print(lottoNumberscondensed)
for item2 in lottoMega[0:1]:
    print("Todays Mega Numer is: ")
    print(item2)

3 个答案:

答案 0 :(得分:4)

很高兴听到您正在学习Python,欢迎使用StackOverflow!您可能已经听说过,如果必须多次键入某些内容,则可能是做错了什么,而您的代码就是一个很好的示例,说明如何使用适当的工具简化操作。

您要计算随机数。已经有Counter个对象(可以在collections模块中找到),在这里很理想,但是您想自己构建东西,所以让我们这样做:

我们需要构建一个数字列表,而不是构建大量的硬编码变量(x1x2等)。之所以称为numbers,是因为它比x更好:

numbers = [0] * 71

我将使用while循环,而不是for循环:

for count in range(400):
    number = random.randint(1,70)
    numbers[number] += 1

这就是生成号码所要做的全部。

您可以像这样检查当前号码是否已达到10

    if numbers[number] == 10:
        lotterynumbers.append(number)

,依此类推。我希望这有助于您入门。我不想破坏找出其余部分的乐趣。

答案 1 :(得分:0)

欢迎您,并努力!这是程序的更干净版本,可以达到相同的结果:

    import random

    lotteryNumbers = []
    megaNumber = []

    # For convenience, you can use dictionaries to store key value pairs, for example z = {1: 5, 2: 8, 3: 10}
    # In the example, the key number 1 has a value of 5, the key number 2 has a value of 8, and the key number 3 has a value of 10, so it would be your lucky number. 
    # In Python, dictionaries are represented by two curly brackets like so:
    x = {}
    y = {}

    # Populate the two dictionaries with numbers from 1 to 70 included:
    for i in range(1, 71):
        x[i] = 0  # Each key number will initially have the value of 0;
        y[i] = 0

    # Your main loop, simplified:
    while len(lotteryNumbers)<5:
        # The loop will keep running as long as lotteryNumbers contains less than five numbers.
        # When the five numbers have been appended to the list, the loop will stop.

        number = random.randint(1,70)

        x[number] += 1  # x[number] gets you the key number's value, e.g. z[1] was 5 in the example.

        # There's no need to check the value of all key numbers with every loop
        # You can just check the value that's been updated:
        if x[number] == 10:
            lotteryNumbers.append(number)

    # Now do the same for the y dictionary, only this time you loop until the list contains only one number:
    while len(megaNumber)<1:
        meganumber = random.randint(1,70)
        y[meganumber] += 1
        if y[meganumber] == 10:
            megaNumber.append(meganumber)

    print("Today's Lottery Numbers Are: ")
    print(lotteryNumbers)
    print("Today's MegaNumber is: ")
    print(megaNumber)

我鼓励您更多地了解Python词典,并在编程时利用它们。请查看official documentation和/或this tutorial,以了解更多信息。

要了解如何更“ Pythonly”地编写Python代码,还可以研究PEP 8 style guide

希望这会有所帮助!

答案 2 :(得分:-1)

import random

winningLottoNumbers = []
winningMegaNumbers = []

lottoNumbers = [0] * 70
megaNumbers = [0] * 25

for i in range(400):
     lottoNum = random.randint(0,69)
     lottoNumbers[lottoNum] += 1
     megaNum = random.randint(0,69)
     if(megaNum < 25):
         megaNumbers[megaNum] += 1


for i in range(len(lottoNumbers)):
    if(lottoNumbers[i] >= 10):
        winningLottoNumbers.append(i)

for i in range(len(megaNumbers)):
    if(megaNumbers[i] >= 10):
        winningMegaNumbers.append(i)

print(winningLottoNumbers)
print(winningMegaNumbers)