python课后题9-15 彩票分析

img

from random import choice

number = ['a','f',2,3]
N = 0
ticker_number=[]
while N < 2:
    ticker_number.append(choice(number))
    N += 1

print(f"\n{ticker_number}")

my_ticker = []
#循环次数 I
I = 0
M = 0
while my_ticker != ticker_number:
        I += 1
        while M < 2:
            my_ticker.append(choice(number))
            M += 1

print(I)


请问大佬们,为什么这么做得不到输出结果啊

参考下述代码,望采纳

# 导入random模块
import random

# 创建彩票号码列表
my_ticket = [1, 2, 3, 4, 5, 6]

# 设置计数器
count = 0

# 使用while循环,不断随机生成数字,直到中大奖为止
while True:
    # 随机生成一组彩票号码
    winning_ticket = random.sample(range(1, 50), 6)

    # 如果中了大奖,退出循环
    if winning_ticket == my_ticket:
        break
    
    # 否则计数器+1
    count += 1

# 打印消息,报告中了大奖的次数
print("It took {} tries to win the lottery.".format(count))