Home > Enterprise >  How make a if statement more correctly? (If the random number contains a number from the list, then.
How make a if statement more correctly? (If the random number contains a number from the list, then.

Time:12-04

i want to make a code (Lucky ticket, aka lottery) - with can generate random 6-dight number, after that - programm will check a list (with contain a lucky numbers for win: '11', '22', '33' and etc.) and say - are you win or not. But - theres one problem, i cant make if statement correctly, it always gives me error, not right result with i want. List are contain 9 values: luckynumber = '11', '22', '33', '44', '55', '66', '77', '88', '99'.

CodePudding user response:

try this:

if luckynumber in ["put all the lucky numbers in this list"]:
   pass # do whatever you want

CodePudding user response:

One problem you might be having is that in order to compare random_numberwith lucky_numbers, they both need to be strings i.e.,

lucky_numbers = ['11', '22', '33', '44', '55', '66', '77', '88', '99']
random_number = str(random_number) # assuming you already made random_number

You can then compare the two with any(), e.g.

result = any(r in random_number for r in lucky_number)

If you don't convert to random_number to a string, you'll get the error TypeError: argument of type 'int' is not interable

  • Related