Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to compare return values to list

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading