今天学习完,随手练习了下函数的的定义,说说要求:
输入要购买彩票的数量,模拟福彩3D的号码的生成,模拟彩票开奖情况,并判断是否中奖,中奖后中奖号码的数量,及中奖金额。
说说思路:
1.定义生成彩票的函数。随机生成3个0-9的数字,并添加到列表中,生成1个3D号码,再将这个号码添加到总的列表中。
def lottery(num):
list_sum=[] #定义总的彩票购买池
for i in range(num): #循环购买次数
list = [] #定义空的每一注彩票
for j in range(3):
x = random.randint(0,9)
list.append(x) #生成新的彩票
list_sum.append(list) #将彩票添加到彩票池中
return list_sum
2.定义打印彩票的函数。利用for语句循环打印二维列表,代码如下:
#定义彩票打印函数
def lottery_print(li):
for i in range(len(li)):
for j in range(len(li[i])):
print(li[i][j],end=" ")
print()
3. winning if the function is defined. If the winning numbers in the procurement list, the winning judgment, and the final number of the winning numbers and the amount.
def win(buy,num):
if num in buy: #如果中奖号码在购买列表中,判断中奖
count=0
for i in buy: #统计中奖号码数量
if i== num:
count+=1
print("恭喜您中奖了,中奖号码为:",num,"共中得",count,"注")
print("中奖金额为:",count*1000,"元")
else:print("未中奖,感谢您对中国福利事业做出的贡献")
4. Basic functions are defined over, calling on it to simulate winning, as it is buying a note on it! code show as below:
item=int(input("请输入要购买的彩票数量"))
print("您购买的彩票如下:")
buy1=lottery(item)
lottery_print(buy1)
money=len(buy1)*2
print("您已经购买",len(buy1),"注彩票,共花费",money,"元")
print("今日彩票开奖号码为:")
num=lottery(1)
lottery_print(num)
win(buy1,num[0])
White started, the code is simple enough, great God please guide!