我只是在我的 python 书中完成练习,我正在努力完成。
一开始,我应该列出一个包含 10 个数字和 5 个字母的列表。随机选择4个元素并打印有关中奖彩票的消息。
from random import choice, branding numbers_and_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3', '4', '5'] print("If you've got those 4 numbers or letters you've won!!!") for i in range(1, 5): print(choice(numbers_and_letters))
然后我想绕一圈,看看赢得我刚刚创造的那种彩票有多难。我需要制作一个名为my_ticket的列表,然后编写一个循环,不断拉动数字,直到彩票获胜。打印一条消息,报告循环必须调整多少次才能提供中奖彩票。
from random import choice from itertools import count numbers_and_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3', '4', '5'] winning_numbers = [] my_ticket = ['a','1', '5', 'j'] for i in count(): #infinite loop for i in range(1, 5): # four elements from the numbers_and_letters list i = choice(numbers_and_letters) winning_numbers.append(i) print(winning_numbers) if sorted(winning_numbers) != sorted(my_ticket): winning_numbers.clear() elif sorted(winning_numbers) == sorted(my_ticket): print('The numbers are identical') break
我唯一需要做的就是计算代码通过循环集成的次数。我知道我需要使用 enumerate((,但是,我不确定如何将其应用于我的代码。
您已经在计算在count()
上找到与循环匹配项所需的迭代次数。但是,稍后您将用其他值覆盖从该循环中获得的值,因为您将多次重用变量名称i
。
尝试为这三行中的变量使用不同的名称,而不是重用i
:
for draw_count in count():
for character_count in range(1, 5):
character = choice(numbers_and_letters)
现在您可以稍后使用draw_count
。您可能希望打印出draw_count + 1
,因为默认情况下itertools.count
迭代器从零开始。
enumerate()
函数获取一个列表并从列表中的所有元素制作元组,如[(0, elem1), (1, elem2), ...]
。
因此,您可以使用它来计算循环像这样运行的次数:
for index, i in enumerate(count()): #infinite loop
for i in range(1, 5): # four elements from the numbers_and_letters list
i = choice(numbers_and_letters)
winning_numbers.append(i)
print(winning_numbers)
if sorted(winning_numbers) != sorted(my_ticket):
winning_numbers.clear()
elif sorted(winning_numbers) == sorted(my_ticket):
print('The numbers are identical')
print('It took %d runs!' % (index + 1))
break
索引 + 1,因为索引以 0 开头。
您可以使用临时计数器cnt
来跟踪循环从列表中提取元素并将其与票证匹配的次数 首先在循环外用 0 初始化它
然后在每次循环运行时递增cnt
,如果找到匹配项print(cnt)
,然后再次将cnt设置为0(在if内(以进行下一次迭代
无需使用枚举。"计数"很好。
以下是对使其工作的代码的轻微修改:
from random import choice
from itertools import count
numbers_and_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'1', '2', '3', '4', '5']
winning_numbers = []
my_ticket = ['a','1', '5', 'j']
def get_random_ticket():
res = []
for i in range(4): # four elements from the numbers_and_letters list
i = choice(numbers_and_letters)
res.append(i)
return res
winning_numbers = get_random_ticket()
print(winning_numbers)
for i in count(): #infinite loop
if sorted(winning_numbers) == sorted(get_random_ticket()):
print('The numbers are identical')
print(i)
break
要计算代码通过循环集成的次数,只需添加一个计数器。
from random import choice
from itertools import count
numbers_and_letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'1', '2', '3', '4', '5']
winning_numbers = []
my_ticket = ['a','1', '5', 'j']
a = 0
for i in count(): #infinite loop
for i in range(1, 5): # four elements from the numbers_and_letters list
i = choice(numbers_and_letters)
winning_numbers.append(i)
print(winning_numbers)
a += 1
if sorted(winning_numbers) != sorted(my_ticket):
winning_numbers.clear()
elif sorted(winning_numbers) == sorted(my_ticket):
print('The numbers are identical')
print(a)
break
这是输出(点表示有几个打印winning_numbers(-
......
......
......
......
['4', 'd', 'h', '3']
['h', 'd', 'h', 'j']
['5', 'd', '5', 'c']
['1', '5', 'j', 'a']
The numbers are identical
2852