正在尝试比较要列出的 return 个值

Trying to compare return values to list

randomgenerator() 是一个产生 6 个随机整数的函数,提示用户也输入 6 个值,这些值将添加到 lucky[]。 我想将每个收益值与 lucky[] 列表进行比较以获得匹配项,但不满足 if 条件。

for x in randomgenerator():
      print(f"\n{x} is a winning number.")
      if x in lucky:
           print(f"There is a match with number {x}")
           match.append(x)
def randomgenerator():
    for i in range(5):
        yield random.randint(1,2)
    yield random.randint(1,2)

您说 randomgenerator returns 包含 inttuple

randomgenerator() is a function that yields 6 random values

我猜你所说的“值”是指 integers,因为生成随机 strings 非常奇怪。

然后lucky填充input()个返回值,也就是strings.


if str(x) in lucky: # This should work, since it will convert the int x to a string

类似的解决方案是:

lucky = list(map(int, lucky)) # all the elements are converted to integers