双色球和大乐透的随机选号并排列

总想试做一点适用的东西,太深了不懂,先拿这个试试。写完后发现代码有点臃肿。

import random time

def loottery():

    while 1:

        game = input('请输入您要投注的玩法:【1.双色球】 【2.大乐透】') 

        if game == str(1):

            i = 34
            i1 = int(input('机选几个红球(单式投注为6个):'))
            j = 17
            j1 = int(input('机选几个蓝球(单式投注为1个):'))

            if i1 <= 20 and j1 <= 16:
                pass
            elif i1 > 20 or j1 > 16:
                print('出错了!复式投注最多选红球20个,蓝球16个。')
                continue
            
            redball = random.sample(range(1,i),i1)
            redball.sort()
            blueball = random.sample(range(1,j),j1)
            redball.sort()

        elif game == str(2):
            k = 36
            k1 = int(input('机选几个红球(单式投注为5个):'))
            m = 13
            m1 = int(input('机选几个蓝球(单式投注为2个):'))

            if k1 <= 18 and m1 <= 9:
                pass
            elif k1 > 18 or m1 > 9:
                print('出错了!复式投注最多选红球18个,蓝球9个。')
                continue
            
            redball = random.sample(range(1,k),k1)
            redball.sort()
            blueball = random.sample(range(1,m),m1)
            redball.sort()

        last = redball + blueball
        print('您机选的投注结果为\n' + str(last)+'\n祝您好运!')

        ifconti = input('还继续选吗?【输入y继续】【其他键退出】:')
        if ifconti == 'y':
            continue
        else:
            break

loottery()    

你可能感兴趣的:(练习)