鱼C论坛

 找回密码
 立即注册
查看: 1579|回复: 13

[已解决]模拟大乐透中奖

[复制链接]
发表于 2021-9-11 22:04:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
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")
最佳答案
2021-9-11 22:34:51
import random

print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
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 = [random.randint(1, 69) for i in range(5)]
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")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 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 [not identical, between 1-69] and 1 red ball [between 1-26]
然后再来进行匹配,看我中了几个号码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-11 22:20:55 | 显示全部楼层
求大佬帮忙一下,怎么在两组不同的数列里来匹配对应的几个数字啊
感激不尽!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-11 22:34:51 | 显示全部楼层    本楼为最佳答案   
import random

print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
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 = [random.randint(1, 69) for i in range(5)]
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")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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 [not identical, between 1-35] and 2 red ball [between 1-12]")
print("For example: 01 02 03 04 05 16 17")
ticket = input('Input your ticket numbers: ')
ticket = [int(x) for x in ticket.split(' ')]
print('Your tikcet:', ticket)
fc = 0  # front zone match counter
bc = 0  # back zone match counter
for i in range(5):
    if ticket[i] in fzone:
        fc += 1
for i in range(2):
    if ticket[i + 4] 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!')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-11 23:15:14 | 显示全部楼层

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

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

white = [32, 35, 40, 52, 54]
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")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-11 23:15:53 | 显示全部楼层
本帖最后由 傻眼貓咪 于 2021-9-11 23:39 编辑
import random

def game():
    """
    Buy a ticket with 5 white balls [not identical, between 1-69]
    and 1 red ball [between 1-26]
    """
    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 = [int(input(f"Your {string[i]} 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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

其實根本不用改啊,你直接輸入:[32, 35, 40, 52, 54] 不就得了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-12 17:09:48 | 显示全部楼层
傻眼貓咪 发表于 2021-9-11 23:26
其實根本不用改啊,你直接輸入:[32, 35, 40, 52, 54] 不就得了?

那请问一下大佬如果需要算至少买多少次彩票才能中至少3个白球,怎么做啊? 能教教我吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-12 17:22:45 | 显示全部楼层
用 while,重複輸入 [32, 35, 40, 52, 54],直到達到目標至少中三顆白球為止(做次數記錄每當輸入一次),最後返回次數便可
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-12 17:40:13 | 显示全部楼层
傻眼貓咪 发表于 2021-9-12 17:22
用 while,重複輸入 [32, 35, 40, 52, 54],直到達到目標至少中三顆白球為止(做次數記錄每當輸入一次), ...

不太明白,大佬能再详细点吗
感激不尽!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-12 18:28:13 | 显示全部楼层

大佬,我发现一个问题。我运行到有一次是只中了三个白球,但是显示结果是我中了五个白球。我是不是该在下面改成matchwhite == 5?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-12 18:41:52 | 显示全部楼层
Ddaniel1177 发表于 2021-9-12 17:40
不太明白,大佬能再详细点吗
感激不尽!!

你說 [32, 35, 40, 52, 54] 其實就是中獎號碼對嗎?
至於你買的樂透號碼是隨機的,所以只要重複輸入中獎號碼然後對照隨機號碼(如有中至少三顆白球就退出),算出一共要輸入多少次不就是你要的答案了?

代碼流程:(只是舉例,不要當真,樂透不可能三次中獎的!)
這裡看你想用 while,或是你手動輸入隨你喜歡,都一樣

輸入: [32, 35, 40, 52, 54] (中獎號碼)
執行代碼隨機生成號碼比如:[32, 60, 35, 15, 24]  只中兩顆,失敗

再輸入: [32, 35, 40, 52, 54] (中獎號碼)
執行代碼隨機生成號碼比如:[23, 18, 7, 15, 54]  只中一顆,失敗

再輸入: [32, 35, 40, 52, 54] (中獎號碼)
執行代碼隨機生成號碼比如:[23, 54, 7, 40, 32]  中三顆,成功

答案:一共三次

個人認為你的題目給定中獎號碼,然後自己花錢買的樂透不能選號碼,本身題目就怪怪的,
通常中獎號碼是隨機的,而你買的樂透號碼是可選的,這才是正常操作啊~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

因为逻辑判断比较复杂,可以根据实际运行情况修改程序。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-13 10:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表