Home > Back-end >  Trying to compare return values to list
Trying to compare return values to list

Time:02-08

randomgenerator() is a function that yields 6 random integers, the user is prompted to also enter 6 values which are added to lucky[]. I want to compare each yield value to the lucky[] list for a match, but the if condition is not being met.

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)

CodePudding user response:

You said randomgenerator returns a tuple containing ints.

randomgenerator() is a function that yields 6 random values

I guess by "values" you meant integers, since it's very strange to generate random strings.

Then lucky is filled with input() returned values, which are strings.


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

a similar solution would be:

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