鱼C论坛

 找回密码
 立即注册
查看: 1812|回复: 12

[已解决]写了一段随机取数的程序,模拟彩票选号,然后报错说没有红球这个定义,麻烦指点

[复制链接]
发表于 2020-6-10 20:57:14 | 显示全部楼层 |阅读模式

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

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

x
  1. #买彩票
  2. import random
  3. import math
  4. red = []
  5. blue = []
  6. print('请问老板是玩双色球还是大乐透?')
  7. print('双色球请输入1,大乐透请输入2。')
  8. way = int(input('请输入您的选择: '))

  9. if int(way) == 1 :
  10.     for n in range(1,33):
  11.         if len(red) <= 29:
  12.             n = random.randint(1,32)
  13.             if n in red:
  14.                 continue
  15.             red.append(n)
  16.         if len(blue) <=4:
  17.             m = random.randint(1,16)
  18.             if m in blue:
  19.                 continue
  20.             blue.append(m)
  21.     print('这次的幸运号分别是是:','第一注红球'red.[0:5],'第一注蓝球',blue.[0],/n)
  22.     print('                      ','第二注红球'red.[6:11],'第二注蓝球',blue.[1],/n)
  23.     print('                      ','第三注红球'red.[12:16],'第三注蓝球',blue.[2],/n)
  24.     print('                      ','第四注红球'red.[17:21],'第四注蓝球',blue.[3],/n)
  25.     print('                      ','第五注红球'red.[22:27],'第五注蓝球',blue.[4],/n)
  26. elif int(way) == 2 :
  27.     for n in range(1,34):
  28.         if len(red) <= 19:
  29.             n = random.randint(1,34)
  30.             if n in red:
  31.                 continue
  32.             red.append(n)
  33.         if len(blue) <=9:
  34.             m = random.randint(1,12)
  35.             if m in blue:
  36.                 continue
  37.             blue.append(m)
  38.     print('这次的幸运号分别是是:','第一注红球'red.[0:4],'第一注蓝球',blue.[0:1],/n)
  39.     print('                      ','第二注红球'red.[5:9],'第二注蓝球',blue.[2:3],/n)
  40.     print('                      ','第三注红球'red.[10:14],'第三注蓝球',blue.[4:5],/n)
  41.     print('                      ','第四注红球'red.[15:19],'第四注蓝球',blue.[6:7],/n)
  42.     print('                      ','第五注红球'red.[20:24],'第五注蓝球',blue.[8:9],/n)

  43. else:
  44.     print('老板请不要开玩笑。')
  45. input('按回车结束程序: ')
复制代码


感谢!
最佳答案
2020-6-10 21:10:11
本帖最后由 Twilight6 于 2020-6-10 22:20 编辑

问题蛮多的:


  • way 已经在 input 转为 int 了,不用再次 int
  • 这个地方的red前面需要有个逗号,red 后面 不能加了个 '.' ,还有后面的 /n 需要写成字符串,因为/n是字符串转义字符,表换行作用

    1. print('这次的幸运号分别是是:','第一注红球'red.[0:5],'第一注蓝球',blue.[0],/n)
    复制代码


    改成:

    1.     print('这次的幸运号分别是是:','第一注红球',red[0:5],'第一注蓝球',blue[0],'/n')
    复制代码


    这样这里就不会报错了
  • 列表切片用法错误,你red[0:5] 是取了 red 列表里的 0~4个元素了
  • 问题有点小多....很多用法都错了


帮你纠正了下语法错误的代码,可以正常运行了,你自己想想办法达到你自己的目的吧:

  1. #买彩票
  2. import random
  3. import math
  4. red = []
  5. blue = []
  6. print('请问老板是玩双色球还是大乐透?')
  7. print('双色球请输入1,大乐透请输入2。')
  8. way = int(input('请输入您的选择: '))

  9. if way == 1 :
  10.     for n in range(1,33):
  11.         if len(red) <= 29:
  12.             n = random.randint(1,32)
  13.             if n in red:
  14.                 continue
  15.             red.append(n)
  16.         if len(blue) <=4:
  17.             m = random.randint(1,16)
  18.             if m in blue:
  19.                 continue
  20.             blue.append(m)
  21.     print('这次的幸运号分别是是:','第一注红球',red[0:5],'第一注蓝球',blue[0],'/n')
  22.     print('                      ','第二注红球',red[6:11],'第二注蓝球',blue[1],'/n')
  23.     print('                      ','第三注红球',red[12:16],'第三注蓝球',blue[2],'/n')
  24.     print('                      ','第四注红球',red[17:21],'第四注蓝球',blue[3],'/n')
  25.     print('                      ','第五注红球',red[22:27],'第五注蓝球',blue[4],'/n')
  26. elif way == 2 :
  27.     for n in range(1,34):
  28.         if len(red) <= 19:
  29.             n = random.randint(1,34)
  30.             if n in red:
  31.                 continue
  32.             red.append(n)
  33.         if len(blue) <=9:
  34.             m = random.randint(1,12)
  35.             if m in blue:
  36.                 continue
  37.             blue.append(m)
  38.     print('这次的幸运号分别是是:','第一注红球',red[0:4],'第一注蓝球',blue[0:1],'/n')
  39.     print('                      ','第二注红球',red[5:9],'第二注蓝球',blue[2:3],'/n')
  40.     print('                      ','第三注红球',red[10:14],'第三注蓝球',blue[4:5],'/n')
  41.     print('                      ','第四注红球',red[15:19],'第四注蓝球',blue[6:7],'/n')
  42.     print('                      ','第五注红球',red[20:24],'第五注蓝球',blue[8:9],'/n')

  43. else:
  44.     print('老板请不要开玩笑。')
  45. input('按回车结束程序: ')
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-10 21:02:57 | 显示全部楼层
way已经是整数了,不需要int(way)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 21:10:11 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-6-10 22:20 编辑

问题蛮多的:


  • way 已经在 input 转为 int 了,不用再次 int
  • 这个地方的red前面需要有个逗号,red 后面 不能加了个 '.' ,还有后面的 /n 需要写成字符串,因为/n是字符串转义字符,表换行作用

    1. print('这次的幸运号分别是是:','第一注红球'red.[0:5],'第一注蓝球',blue.[0],/n)
    复制代码


    改成:

    1.     print('这次的幸运号分别是是:','第一注红球',red[0:5],'第一注蓝球',blue[0],'/n')
    复制代码


    这样这里就不会报错了
  • 列表切片用法错误,你red[0:5] 是取了 red 列表里的 0~4个元素了
  • 问题有点小多....很多用法都错了


帮你纠正了下语法错误的代码,可以正常运行了,你自己想想办法达到你自己的目的吧:

  1. #买彩票
  2. import random
  3. import math
  4. red = []
  5. blue = []
  6. print('请问老板是玩双色球还是大乐透?')
  7. print('双色球请输入1,大乐透请输入2。')
  8. way = int(input('请输入您的选择: '))

  9. if way == 1 :
  10.     for n in range(1,33):
  11.         if len(red) <= 29:
  12.             n = random.randint(1,32)
  13.             if n in red:
  14.                 continue
  15.             red.append(n)
  16.         if len(blue) <=4:
  17.             m = random.randint(1,16)
  18.             if m in blue:
  19.                 continue
  20.             blue.append(m)
  21.     print('这次的幸运号分别是是:','第一注红球',red[0:5],'第一注蓝球',blue[0],'/n')
  22.     print('                      ','第二注红球',red[6:11],'第二注蓝球',blue[1],'/n')
  23.     print('                      ','第三注红球',red[12:16],'第三注蓝球',blue[2],'/n')
  24.     print('                      ','第四注红球',red[17:21],'第四注蓝球',blue[3],'/n')
  25.     print('                      ','第五注红球',red[22:27],'第五注蓝球',blue[4],'/n')
  26. elif way == 2 :
  27.     for n in range(1,34):
  28.         if len(red) <= 19:
  29.             n = random.randint(1,34)
  30.             if n in red:
  31.                 continue
  32.             red.append(n)
  33.         if len(blue) <=9:
  34.             m = random.randint(1,12)
  35.             if m in blue:
  36.                 continue
  37.             blue.append(m)
  38.     print('这次的幸运号分别是是:','第一注红球',red[0:4],'第一注蓝球',blue[0:1],'/n')
  39.     print('                      ','第二注红球',red[5:9],'第二注蓝球',blue[2:3],'/n')
  40.     print('                      ','第三注红球',red[10:14],'第三注蓝球',blue[4:5],'/n')
  41.     print('                      ','第四注红球',red[15:19],'第四注蓝球',blue[6:7],'/n')
  42.     print('                      ','第五注红球',red[20:24],'第五注蓝球',blue[8:9],'/n')

  43. else:
  44.     print('老板请不要开玩笑。')
  45. input('按回车结束程序: ')
复制代码

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

使用道具 举报

发表于 2020-6-10 22:16:24 | 显示全部楼层

补充一下哈,换行符是'\n';另外print默认是会打印完后换行的,除非希望每行之间隔一个空行,否者这里加入换行符是多余的;最后这种多行输出,三引号是最佳选择。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 22:47:34 | 显示全部楼层
楼主除了对切片理解错误,似乎对彩票的规则也理解错误,我猜题目应该是使用的单式投注法,以双色球来说,每一注的6个红球是不会有重复的,而楼主的写法是5注的所有红球都不会有重复(而且代码切片有误,有的是4个球,有的是5个球),这样如果需要第6注彩票,抱歉,没有足够的球了。所以正确的写法应该是每次循环算出6个红球和1个蓝球,每次循环开始都要把数据复位清零。下面大乐透也是这样,只是变成了5+2。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 23:52:17 | 显示全部楼层
1.way不需要重复的使用int,输入的时候用了就可以了
2.print输出末尾默认是有一个回车的
3.red前面缺少一个分隔符“,”
4.列表中取元素,不用在名字后面加“.“
不是很了解双色球,输出大致如下
  1. print('这次的幸运号分别是是:','第一注红球',red[:5],'第一注蓝球',blue[0])   
  2.     print('                      ','第二注红球',red[5:10],'第二注蓝球',blue[1])
  3.     print('                      ','第三注红球',red[10:16],'第三注蓝球',blue[2])
  4.     print('                      ','第四注红球',red[16:21],'第四注蓝球',blue[3])
  5.     print('                      ','第五注红球',red[21:],'第五注蓝球',blue[4])
复制代码



下面是我搞得选powerball的一个小程序
  1. #这是一个随机选择数字的游戏
  2. import random
  3. selectball = random.randrange(1,70,1)
  4. # redball = random.randrange(1,27)
  5. ballnumber = 0
  6. ball = [0,0,0,0,0]
  7. while ballnumber < 5:
  8.     ball[ballnumber] = selectball
  9.     print ('第',ballnumber,'球是:',selectball)
  10.     ballnumber += 1
  11.     selectball = random.randrange(1,70)
  12.     while 1:
  13.         if selectball in ball[:]:
  14.             selectball =random.randrange(1,70)
  15.         else :
  16.             break
  17. redball = random.randrange(1,27)
  18. print ('特殊球是:',redball)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-11 00:13:06 | 显示全部楼层
  1. #买彩票
  2. import random

  3. def lottery(kind):
  4.     if kind==1:
  5.         redmax,bluemax,redcount,bluecount=33,16,6,1
  6.     elif kind==2:
  7.         redmax,bluemax,redcount,bluecount=35,12,5,2
  8.     else:
  9.         return
  10.     red=[]
  11.     blue=[]
  12.     j,k=0,0
  13.     while j<redcount:
  14.         n = random.randint(1,redmax)
  15.         if n in red:
  16.             continue
  17.         j+=1
  18.         red.append(n)
  19.     while k<bluecount:
  20.         n = random.randint(1,bluemax)
  21.         if n in blue:
  22.             continue
  23.         k+=1
  24.         blue.append(n)
  25.     return (red,blue)
  26.             
  27. print('请问老板是玩双色球还是大乐透?\n双色球请输入1,大乐透请输入2,退出请输入0')
  28. while True:
  29.     way = input('请输入您的选择:')
  30.     if not way.isdigit():
  31.         print('输入错误,',end='')
  32.         continue
  33.     way=int(way)
  34.     if way==0:
  35.         break
  36.     if 0<way<3:
  37.         print('这次的幸运号分别是:',end='')
  38.         for i in range(5):
  39.             no=lottery(way)
  40.             if i!=0:
  41.                 print(' '*17,end='')
  42.             print('第%s注红球 %s 第%s注蓝球 %s' % ('一二三四五'[i],' '.join([str(j) for j in no[0]]),'一二三四五'[i],' '.join([str(j) for j in no[1]])))
  43.     else:
  44.         print('老板请不要开玩笑,',end='')
  45.         continue
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-11 17:04:36 | 显示全部楼层

哇~感谢大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-11 17:05:25 | 显示全部楼层
txxcat 发表于 2020-6-10 22:47
楼主除了对切片理解错误,似乎对彩票的规则也理解错误,我猜题目应该是使用的单式投注法,以双色球来说,每 ...

感谢感谢!一会晚上再仔细研究!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-11 17:06:11 | 显示全部楼层
lucky邪神 发表于 2020-6-10 23:52
1.way不需要重复的使用int,输入的时候用了就可以了
2.print输出末尾默认是有一个回车的
3.red前面缺少一 ...

感谢感谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-11 17:07:17 | 显示全部楼层

客气了~  加油吧

                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-11 21:53:40 | 显示全部楼层

完成

本帖最后由 imcampbell 于 2020-6-11 22:00 编辑


大佬,我有新问题!
  1. #买彩票
  2. import random
  3. import math
  4. red = []
  5. blue = []
  6. print('请问老板是玩双色球还是大乐透?')
  7. print('双色球请输入1,大乐透请输入2。')
  8. way = int(input('请输入您的选择: '))

  9. if way == 1 :
  10.     for n in range(1,33):
  11.         if len(red) < 30:
  12.             n = random.randint(1,32)
  13.             if n in red:
  14.                 continue
  15.             red.append(n)
  16.         if len(blue) <=4:
  17.             m = random.randint(1,16)
  18.             if m in blue:
  19.                 continue
  20.             blue.append(m)
  21.     print('红球有',red[:],'蓝球有',blue[:])
  22.     print('这次的幸运号分别是是:','第一注红球',red[0:6],'第一注蓝球',blue[0])
  23.     print('                      ','第二注红球',red[6:12],'第二注蓝球',blue[1])
  24.     print('                      ','第三注红球',red[12:18],'第三注蓝球',blue[2])
  25.     print('                      ','第四注红球',red[18:24],'第四注蓝球',blue[3])
  26.     print('                      ','第五注红球',red[24:30],'第五注蓝球',blue[4])
  27. elif way == 2 :
  28.     for n in range(1,34):
  29.         if len(red) < 24:
  30.             n = random.randint(1,34)
  31.             if n in red:
  32.                 continue
  33.             else: red.append(n)
  34.     for m in range(1,12):
  35.         if len(blue) <=10:
  36.             m = random.randint(1,12)
  37.             if m in blue:
  38.                 continue
  39.             else: blue.append(m)
  40.     print('红球有',red[:],'蓝球有',blue[:])
  41.     print('这次的幸运号分别是是:','第一注红球',red[0:5],'第一注蓝球',blue[0:2])
  42.     print('                      ','第二注红球',red[5:10],'第二注蓝球',blue[2:4])
  43.     print('                      ','第三注红球',red[10:15],'第三注蓝球',blue[4:6])
  44.     print('                      ','第四注红球',red[15:20],'第四注蓝球',blue[6:8])
  45.     print('                      ','第五注红球',red[20:25],'第五注蓝球',blue[8:10])

  46. else:
  47.     print('老板请不要开玩笑。')
  48. input('按回车结束程序: ')
复制代码

这个是更新后的程序。
我的想法是,比如双色球,我要从1到32个数中取30个不同的随机数,随机排列,作为红球,然后从1到16中间随机取5个不同的数,随机排列,作为蓝球。
红球中的1到6号,作为第一注,蓝球中的1号,作为第一注。依次类推。
现在的问题是,我的程序取不满24个不同随机数。
第二部分大乐透也是,取不满20个随机排列的不同数字。还请大佬进一步指点一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-27 08:57:50 | 显示全部楼层
应该是'\n'不是'/n'!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 10:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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