鱼C论坛

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

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

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

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

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

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

  2. print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
  3. a = int(input("Your first number for white balls\n"))
  4. b = int(input("Your second number for white balls\n"))
  5. c = int(input("Your third number for white balls\n"))
  6. d = int(input("Your forth number for white balls\n"))
  7. e = int(input("Your fiveth number for white balls\n"))
  8. f = int(input("Your number for red ball\n"))

  9. white1 = random.sample(range(1, 69), 1)
  10. white2 = random.sample(range(1, 69), 1)
  11. white3 = random.sample(range(1, 69), 1)
  12. white4 = random.sample(range(1, 69), 1)
  13. white5 = random.sample(range(1, 69), 1)
  14. red = random.randint(1, 26)

  15. print("Your selection for white balls are: ", a, b, c, d, e, )
  16. print("Your selection for red balls is: ", f )
  17. print("Winning numbers for white balls:", white1, white2, white3, white4, white5)
  18. 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
  1. import random

  2. print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
  3. mywhite = [int(input("Your first number for white balls\n")),
  4.              int(input("Your second number for white balls\n")),
  5.              int(input("Your third number for white balls\n")),
  6.              int(input("Your forth number for white balls\n")),
  7.              int(input("Your fiveth number for white balls\n"))]
  8. myred = int(input("Your number for red ball\n"))

  9. white = [random.randint(1, 69) for i in range(5)]
  10. red = random.randint(1, 26)
  11. print("Your selection for white balls are: ", *mywhite )
  12. print("Your selection for red balls is: ", myred )
  13. print("Winning numbers for white balls:", *white)
  14. print("Winning number for red ball:", red)
  15. matchwhite = 0
  16. for i, j in zip(mywhite, white):
  17.     if i == j:
  18.         matchwhite += 1
  19. matchred = 1 if myred == red else 0
  20. if matchwhite == 0 and matchred ==0:
  21.     print ("No matching. Thanks for your contribution!")
  22. elif matchred:
  23.     if matchwhite == 0:
  24.         print ("Matching only the red ball: $4")
  25.     elif matchwhite == 1:
  26.         print ("Matching the red ball and one white ball: $4")
  27.     elif matchwhite == 2:
  28.         print ("Matching the red ball and two white balls: $7")
  29.     elif matchwhite == 3:
  30.         print ("Matching the red ball and three white balls: $100")
  31.     elif matchwhite == 4:
  32.         print ("Matching the red ball and four white balls: $10,000")
  33.     else:
  34.         print ("Matching five white balls and the red ball: Jackpot")
  35. else:
  36.     if matchwhite == 3:
  37.         print ("Matching three white balls: $7")
  38.     elif matchwhite == 4:
  39.         print ("Matching four white balls: $100")
  40.     else:
  41.         print ("Matching five white balls: $1,000,000")
复制代码
小甲鱼最新课程 -> https://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]
然后再来进行匹配,看我中了几个号码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

  2. print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
  3. mywhite = [int(input("Your first number for white balls\n")),
  4.              int(input("Your second number for white balls\n")),
  5.              int(input("Your third number for white balls\n")),
  6.              int(input("Your forth number for white balls\n")),
  7.              int(input("Your fiveth number for white balls\n"))]
  8. myred = int(input("Your number for red ball\n"))

  9. white = [random.randint(1, 69) for i in range(5)]
  10. red = random.randint(1, 26)
  11. print("Your selection for white balls are: ", *mywhite )
  12. print("Your selection for red balls is: ", myred )
  13. print("Winning numbers for white balls:", *white)
  14. print("Winning number for red ball:", red)
  15. matchwhite = 0
  16. for i, j in zip(mywhite, white):
  17.     if i == j:
  18.         matchwhite += 1
  19. matchred = 1 if myred == red else 0
  20. if matchwhite == 0 and matchred ==0:
  21.     print ("No matching. Thanks for your contribution!")
  22. elif matchred:
  23.     if matchwhite == 0:
  24.         print ("Matching only the red ball: $4")
  25.     elif matchwhite == 1:
  26.         print ("Matching the red ball and one white ball: $4")
  27.     elif matchwhite == 2:
  28.         print ("Matching the red ball and two white balls: $7")
  29.     elif matchwhite == 3:
  30.         print ("Matching the red ball and three white balls: $100")
  31.     elif matchwhite == 4:
  32.         print ("Matching the red ball and four white balls: $10,000")
  33.     else:
  34.         print ("Matching five white balls and the red ball: Jackpot")
  35. else:
  36.     if matchwhite == 3:
  37.         print ("Matching three white balls: $7")
  38.     elif matchwhite == 4:
  39.         print ("Matching four white balls: $100")
  40.     else:
  41.         print ("Matching five white balls: $1,000,000")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-11 22:42:08 | 显示全部楼层
本帖最后由 suchocolate 于 2021-9-11 22:49 编辑

中国的大乐透
  1. import random

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

  8. print("Buy a ticket with 5 white balls [not identical, between 1-35] and 2 red ball [between 1-12]")
  9. print("For example: 01 02 03 04 05 16 17")
  10. ticket = input('Input your ticket numbers: ')
  11. ticket = [int(x) for x in ticket.split(' ')]
  12. print('Your tikcet:', ticket)
  13. fc = 0  # front zone match counter
  14. bc = 0  # back zone match counter
  15. for i in range(5):
  16.     if ticket[i] in fzone:
  17.         fc += 1
  18. for i in range(2):
  19.     if ticket[i + 4] in bzone:
  20.         bc += 1

  21. if (fc, bc) in [(3, 0), (1, 2), (2, 1), (0, 2)]:
  22.     print('The Ninth award!')
  23. elif (fc, bc) in [(3, 1), (2, 2)]:
  24.     print('The Eighth award!')
  25. elif (fc, bc) in [(4, 0)]:
  26.     print('The Seventh award!')
  27. elif (fc, bc) in [(3, 2)]:
  28.     print('The Sixth award!')
  29. elif (fc, bc) in [(4, 1)]:
  30.     print('The Fifth award!')
  31. elif (fc, bc) in [(4, 2)]:
  32.     print('The Fourth award!')
  33. elif (fc, bc) in [(5, 0)]:
  34.     print('The Third award!')
  35. elif (fc, bc) in [(5, 1)]:
  36.     print('The Second award!')
  37. elif (fc, bc) in [(6, 2)]:
  38.     print('The First award!')
  39. else:
  40.     print('Good luck next time!')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

  5. white = [32, 35, 40, 52, 54]
  6. red = 1
  7. print("Your selection for white balls are: ", *mywhite )
  8. print("Your selection for red balls is: ", myred )
  9. print("Winning numbers for white balls:", *white)
  10. print("Winning number for red ball:", red)
  11. matchwhite = 0
  12. for i, j in zip(mywhite, white):
  13.     if i == j:
  14.         matchwhite += 1
  15. matchred = 1 if myred == red else 0
  16. if matchwhite == 0 and matchred ==0:
  17.     print ("No matching. Thanks for your contribution!")
  18. elif matchred:
  19.     if matchwhite == 0:
  20.         print ("Matching only the red ball: $4")
  21.     elif matchwhite == 1:
  22.         print ("Matching the red ball and one white ball: $4")
  23.     elif matchwhite == 2:
  24.         print ("Matching the red ball and two white balls: $7")
  25.     elif matchwhite == 3:
  26.         print ("Matching the red ball and three white balls: $100")
  27.     elif matchwhite == 4:
  28.         print ("Matching the red ball and four white balls: $10,000")
  29.     else:
  30.         print ("Matching five white balls and the red ball: Jackpot")
  31. else:
  32.     if matchwhite == 3:
  33.         print ("Matching three white balls: $7")
  34.     elif matchwhite == 4:
  35.         print ("Matching four white balls: $100")
  36.     else:
  37.         print ("Matching five white balls: $1,000,000")


复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  2. def game():
  3.     """
  4.     Buy a ticket with 5 white balls [not identical, between 1-69]
  5.     and 1 red ball [between 1-26]
  6.     """
  7.     white = set(random.sample(range(1, 70), 5))
  8.     red = random.choice(range(1, 27))
  9.     string = {1: "first", 2: "second", 3: "third", 4: "forth", 5: "fiveth"}
  10.     myWhite = [int(input(f"Your {string[i]} number for white balls\n")) for i in range(1, 6)]
  11.     myRed = int(input("Your number for red ball\n"))

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

  16.     myWhite = set(myWhite)
  17.     if myRed == red:
  18.         if not (myWhite & white):
  19.             print("Matching only the red ball: $4")
  20.         elif len(myWhite & white) == 1:
  21.             print("Matching the red ball and one white ball: $4")
  22.         elif len(myWhite & white) == 2:
  23.             print("Matching the red ball and two white balls: $7")
  24.         elif len(myWhite & white) == 3:
  25.             print("Matching the red ball and three white balls: $100")
  26.         elif len(myWhite & white) == 4:
  27.             print("Matching the red ball and four white balls: $10,000")
  28.         elif len(myWhite & white) == 5:
  29.             print("Matching five white balls and the red ball: Jackpot")
  30.     elif myRed != red:
  31.         if not (myWhite & white):
  32.             print("No matching. Thanks for your contribution!")
  33.         elif len(myWhite & white) == 3:
  34.             print("Matching three white balls: $7")
  35.         elif len(myWhite & white) == 4:
  36.             print("Matching four white balls: $100")
  37.         elif len(myWhite & white) == 5:
  38.             print("Matching five white balls: $1,000,000")
  39.    
  40. if __name__ == "__main__":
  41.     print(game.__doc__)
  42.     game()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

其實根本不用改啊,你直接輸入:[32, 35, 40, 52, 54] 不就得了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

那请问一下大佬如果需要算至少买多少次彩票才能中至少3个白球,怎么做啊? 能教教我吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

不太明白,大佬能再详细点吗
感激不尽!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

大佬,我发现一个问题。我运行到有一次是只中了三个白球,但是显示结果是我中了五个白球。我是不是该在下面改成matchwhite == 5?
小甲鱼最新课程 -> https://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]  中三顆,成功

答案:一共三次

個人認為你的題目給定中獎號碼,然後自己花錢買的樂透不能選號碼,本身題目就怪怪的,
通常中獎號碼是隨機的,而你買的樂透號碼是可選的,這才是正常操作啊~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

因为逻辑判断比较复杂,可以根据实际运行情况修改程序。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 17:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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