Ddaniel1177 发表于 2021-9-11 22:04:34

模拟大乐透中奖

萌新求大佬们帮帮忙。有一道题说的是模拟购买大乐透中奖号码,然后查看自己买的号码和随机的中奖号码有几个是对应的,中了不同的几个数字需要print出不同的话。1. 想知道如何做到随机中奖号码不会重复;2. 如何用自己的号码去和中奖号码匹配啊?是一个一个按顺序验证吗?
这是我目前写出来的代码,实在是不知道该怎么继续了,求大佬解惑。
import random

print("Buy a ticket with 5 white balls and 1 red ball ")
a = int(input("Your first number for white balls\n"))
b = int(input("Your second number for white balls\n"))
c = int(input("Your third number for white balls\n"))
d = int(input("Your forth number for white balls\n"))
e = int(input("Your fiveth number for white balls\n"))
f = int(input("Your number for red ball\n"))

white1 = random.sample(range(1, 69), 1)
white2 = random.sample(range(1, 69), 1)
white3 = random.sample(range(1, 69), 1)
white4 = random.sample(range(1, 69), 1)
white5 = random.sample(range(1, 69), 1)
red = random.randint(1, 26)

print("Your selection for white balls are: ", a, b, c, d, e, )
print("Your selection for red balls is: ", f )
print("Winning numbers for white balls:", white1, white2, white3, white4, white5)
print("Winning number for red ball:", red)


以下是兑奖条件,if之后该写什么啊
if
    print ("No matching. Thanks for your contribution!")
if
    print ("Matching only the red ball: $4")
if
    print ("Matching the red ball and one white ball: $4")
if
    print ("Matching the red ball and two white balls: $7")
if
    print ("Matching three white balls: $7")
if
    print ("Matching the red ball and three white balls: $100")
if
    print ("Matching four white balls: $100")
if
    print ("Matching the red ball and four white balls: $10,000")
if
    print ("Matching five white balls: $1,000,000")
if
    print ("Matching five white balls and the red ball: Jackpot")

Ddaniel1177 发表于 2021-9-11 22:19:29

不好意思,题目没有说清楚,这个中奖号码是固定的:32 35 40 52 54 for white balls and 01 for red ball,
然后我买的彩票是直接随机生成一组号码:Buy a ticket with 5 white balls and 1 red ball
然后再来进行匹配,看我中了几个号码{:10_266:}

Ddaniel1177 发表于 2021-9-11 22:20:55

求大佬帮忙一下,怎么在两组不同的数列里来匹配对应的几个数字啊{:10_266:}
感激不尽!!

冬雪雪冬 发表于 2021-9-11 22:34:51

import random

print("Buy a ticket with 5 white balls and 1 red ball ")
mywhite = [int(input("Your first number for white balls\n")),
             int(input("Your second number for white balls\n")),
             int(input("Your third number for white balls\n")),
             int(input("Your forth number for white balls\n")),
             int(input("Your fiveth number for white balls\n"))]
myred = int(input("Your number for red ball\n"))

white =
red = random.randint(1, 26)
print("Your selection for white balls are: ", *mywhite )
print("Your selection for red balls is: ", myred )
print("Winning numbers for white balls:", *white)
print("Winning number for red ball:", red)
matchwhite = 0
for i, j in zip(mywhite, white):
    if i == j:
      matchwhite += 1
matchred = 1 if myred == red else 0
if matchwhite == 0 and matchred ==0:
    print ("No matching. Thanks for your contribution!")
elif matchred:
    if matchwhite == 0:
      print ("Matching only the red ball: $4")
    elif matchwhite == 1:
      print ("Matching the red ball and one white ball: $4")
    elif matchwhite == 2:
      print ("Matching the red ball and two white balls: $7")
    elif matchwhite == 3:
      print ("Matching the red ball and three white balls: $100")
    elif matchwhite == 4:
      print ("Matching the red ball and four white balls: $10,000")
    else:
      print ("Matching five white balls and the red ball: Jackpot")
else:
    if matchwhite == 3:
      print ("Matching three white balls: $7")
    elif matchwhite == 4:
      print ("Matching four white balls: $100")
    else:
      print ("Matching five white balls: $1,000,000")

suchocolate 发表于 2021-9-11 22:42:08

本帖最后由 suchocolate 于 2021-9-11 22:49 编辑

中国的大乐透
import random

fzone = random.sample(range(1, 36), 5)
fzone.sort()
bzone = random.sample(range(1, 13), 2)
bzone.sort()
print('fzone:', fzone)
print('bzone:', bzone)

print("Buy a ticket with 5 white balls and 2 red ball ")
print("For example: 01 02 03 04 05 16 17")
ticket = input('Input your ticket numbers: ')
ticket =
print('Your tikcet:', ticket)
fc = 0# front zone match counter
bc = 0# back zone match counter
for i in range(5):
    if ticket in fzone:
      fc += 1
for i in range(2):
    if ticket in bzone:
      bc += 1

if (fc, bc) in [(3, 0), (1, 2), (2, 1), (0, 2)]:
    print('The Ninth award!')
elif (fc, bc) in [(3, 1), (2, 2)]:
    print('The Eighth award!')
elif (fc, bc) in [(4, 0)]:
    print('The Seventh award!')
elif (fc, bc) in [(3, 2)]:
    print('The Sixth award!')
elif (fc, bc) in [(4, 1)]:
    print('The Fifth award!')
elif (fc, bc) in [(4, 2)]:
    print('The Fourth award!')
elif (fc, bc) in [(5, 0)]:
    print('The Third award!')
elif (fc, bc) in [(5, 1)]:
    print('The Second award!')
elif (fc, bc) in [(6, 2)]:
    print('The First award!')
else:
    print('Good luck next time!')

Ddaniel1177 发表于 2021-9-11 23:15:14

冬雪雪冬 发表于 2021-9-11 22:34


谢谢大佬,我根据你的代码,按照题目修改了一下,你看这样是正确的吗?
题目是给了一组固定的中奖号码,然后我自己的号码是直接随机出来一组,我就改了前面一点。
import random

print("Buy a ticket with 5 white balls and 1 red ball ")
mywhite = random.sample(range(1, 69), 5)
myred = random.randint(1, 26)

white =
red = 1
print("Your selection for white balls are: ", *mywhite )
print("Your selection for red balls is: ", myred )
print("Winning numbers for white balls:", *white)
print("Winning number for red ball:", red)
matchwhite = 0
for i, j in zip(mywhite, white):
    if i == j:
      matchwhite += 1
matchred = 1 if myred == red else 0
if matchwhite == 0 and matchred ==0:
    print ("No matching. Thanks for your contribution!")
elif matchred:
    if matchwhite == 0:
      print ("Matching only the red ball: $4")
    elif matchwhite == 1:
      print ("Matching the red ball and one white ball: $4")
    elif matchwhite == 2:
      print ("Matching the red ball and two white balls: $7")
    elif matchwhite == 3:
      print ("Matching the red ball and three white balls: $100")
    elif matchwhite == 4:
      print ("Matching the red ball and four white balls: $10,000")
    else:
      print ("Matching five white balls and the red ball: Jackpot")
else:
    if matchwhite == 3:
      print ("Matching three white balls: $7")
    elif matchwhite == 4:
      print ("Matching four white balls: $100")
    else:
      print ("Matching five white balls: $1,000,000")


傻眼貓咪 发表于 2021-9-11 23:15:53

本帖最后由 傻眼貓咪 于 2021-9-11 23:39 编辑

import random

def game():
    """
    Buy a ticket with 5 white balls
    and 1 red ball
    """
    white = set(random.sample(range(1, 70), 5))
    red = random.choice(range(1, 27))
    string = {1: "first", 2: "second", 3: "third", 4: "forth", 5: "fiveth"}
    myWhite = } number for white balls\n")) for i in range(1, 6)]
    myRed = int(input("Your number for red ball\n"))

    print("Your selection for white balls are: ", *myWhite )
    print("Your selection for red balls is: ", myRed )
    print("Winning numbers for white balls:", *white)
    print("Winning number for red ball:", red)

    myWhite = set(myWhite)
    if myRed == red:
      if not (myWhite & white):
            print("Matching only the red ball: $4")
      elif len(myWhite & white) == 1:
            print("Matching the red ball and one white ball: $4")
      elif len(myWhite & white) == 2:
            print("Matching the red ball and two white balls: $7")
      elif len(myWhite & white) == 3:
            print("Matching the red ball and three white balls: $100")
      elif len(myWhite & white) == 4:
            print("Matching the red ball and four white balls: $10,000")
      elif len(myWhite & white) == 5:
            print("Matching five white balls and the red ball: Jackpot")
    elif myRed != red:
      if not (myWhite & white):
            print("No matching. Thanks for your contribution!")
      elif len(myWhite & white) == 3:
            print("Matching three white balls: $7")
      elif len(myWhite & white) == 4:
            print("Matching four white balls: $100")
      elif len(myWhite & white) == 5:
            print("Matching five white balls: $1,000,000")
   
if __name__ == "__main__":
    print(game.__doc__)
    game()

傻眼貓咪 发表于 2021-9-11 23:26:16

Ddaniel1177 发表于 2021-9-11 23:15
谢谢大佬,我根据你的代码,按照题目修改了一下,你看这样是正确的吗?
题目是给了一组固定的中奖号码, ...

其實根本不用改啊,你直接輸入: 不就得了?{:5_105:}

Ddaniel1177 发表于 2021-9-12 17:09:48

傻眼貓咪 发表于 2021-9-11 23:26
其實根本不用改啊,你直接輸入: 不就得了?

那请问一下大佬如果需要算至少买多少次彩票才能中至少3个白球,怎么做啊? 能教教我吗

傻眼貓咪 发表于 2021-9-12 17:22:45

用 while,重複輸入 ,直到達到目標至少中三顆白球為止(做次數記錄每當輸入一次),最後返回次數便可{:5_91:}

Ddaniel1177 发表于 2021-9-12 17:40:13

傻眼貓咪 发表于 2021-9-12 17:22
用 while,重複輸入 ,直到達到目標至少中三顆白球為止(做次數記錄每當輸入一次), ...

不太明白,大佬能再详细点吗{:10_266:}
感激不尽!!

Ddaniel1177 发表于 2021-9-12 18:28:13

冬雪雪冬 发表于 2021-9-11 22:34


大佬,我发现一个问题。我运行到有一次是只中了三个白球,但是显示结果是我中了五个白球。我是不是该在下面改成matchwhite == 5?

傻眼貓咪 发表于 2021-9-12 18:41:52

Ddaniel1177 发表于 2021-9-12 17:40
不太明白,大佬能再详细点吗
感激不尽!!

你說 其實就是中獎號碼對嗎?
至於你買的樂透號碼是隨機的,所以只要重複輸入中獎號碼然後對照隨機號碼(如有中至少三顆白球就退出),算出一共要輸入多少次不就是你要的答案了?
{:5_103:}
代碼流程:(只是舉例,不要當真,樂透不可能三次中獎的!)
這裡看你想用 while,或是你手動輸入隨你喜歡,都一樣

輸入: (中獎號碼)
執行代碼隨機生成號碼比如:只中兩顆,失敗

再輸入: (中獎號碼)
執行代碼隨機生成號碼比如:只中一顆,失敗

再輸入: (中獎號碼)
執行代碼隨機生成號碼比如:中三顆,成功

答案:一共三次

個人認為你的題目給定中獎號碼,然後自己花錢買的樂透不能選號碼,本身題目就怪怪的,
通常中獎號碼是隨機的,而你買的樂透號碼是可選的,這才是正常操作啊~

冬雪雪冬 发表于 2021-9-12 20:50:20

Ddaniel1177 发表于 2021-9-12 18:28
大佬,我发现一个问题。我运行到有一次是只中了三个白球,但是显示结果是我中了五个白球。我是不是该在下 ...

因为逻辑判断比较复杂,可以根据实际运行情况修改程序。
页: [1]
查看完整版本: 模拟大乐透中奖