``from random import randintfirstnumber=randint (1,9)print(firstnumber)secondnumber=randint (1,9)print(secondnumber)a= str(firstnumber) + str(secondnumber)print (a)numbers= input("Enter 2 numbers")b = int(numbers)if b== int(a):print ("You have won 10000 dollars")elif int(b)==int(a) and int(b)==int(a):print ("You have won 3000 dollars")**elif int(b)== int(a) or int(b)==int(a) or int(b)==int(a) or int(b)== int:**print ("You have won 1000 dollars")else:print ('Try again')``

The program is supposed to determine 2 random numbers and put them together to create the lottery number. These 2 numbers should not be the same and the program should generate another 2 numbers to make a new lottery number, if these 2 numbers are the same. The program should not print the random numbers as this is the lottery winning number.

This is how the reward system would work if the lottery number was 53. If the person guesses the correct numbers in order (ie 53) the persons should win \$10000 , if the person guesses the correct numbers in reverse order (ie 35) they should win \$3000, and if they guess one of the numbers correctly (ie. 37 or 63 or 59 or 45), they should win \$1000. Otherwise the program should print out (Try Again).

I basically want to know what how to fix my program (when the input is anything other then the input which wins them \$10,000 the program fails because their is something wrong with the bolded line. I also want to know how to generate another lottery winning numbers if the 2 numbers randint generates is the same and I also want to know how to check the input to see if the input contains one of the lottery numbers.

There are a number of things that could be improved, but a few definite problems include:

``````int(b)== int
``````

should be

``````int(b)== int(a)
``````
• the condition for winning 3000 dollars should come before the condition for winning 1000 dollars. Since the condition for winning 1000 dollars is also True when the person is eligible for winning 3000 dollars.
• the condition for winning 3000 dollars should be

``````int(b)==int(a) and int(b)==int(a)
``````

(notice the change in the last index), rather than

``````int(b)==int(a) and int(b)==int(a)
``````
• since you are really treating the response as strings rather than as ints, you might as well keep `a` and `b` as strings. Doing so means you can remove all the calls to `int`.

So a working version of your program with minimal changes would be:

``````from random import randint
firstnumber = randint(1, 9)
print(firstnumber)

secondnumber = randint(1, 9)
print(secondnumber)

a = str(firstnumber) + str(secondnumber)
print(repr(a))

b = raw_input("Enter 2 numbers")
print(repr(b))

if b == a:
print ("You have won 10000 dollars")
elif b == a and b == a:
print ("You have won 3000 dollars")
elif (b == a
or b == a
or b == a
or b == a):
print ("You have won 1000 dollars")
else:
print ('Try again')
``````

An easier way to express the conditions would be:

``````if b == a:
print ("You have won 10000 dollars")
elif a == b[::-1]:
print ("You have won 3000 dollars")
elif len(set(b).intersection(a)) == 1:
print ("You have won 1000 dollars")
else:
print ('Try again')
``````

Regarding `len(set(b).intersection(a)) == 1`: When `b` is a string, `set(b)` is the set of characters in the string:

``````In : b = '75'

In : set(b)
Out: {'5', '7'}
``````

Sets have an intersection method which can accept an iterable as input. It returns the set of items from the original set which are also in the iterable. For example,

``````In : set('75').intersection('5')
Out: {'5'}

In : set('75').intersection('baloney')
Out: set()

In : set('75').intersection('57')
Out: {'5', '7'}

In : set('75').intersection()  # strings and ints are not equal
Out: set()
``````

So `len(set(b).intersection(a))` counts the number of characters in `b` which are also in `a` (in any order). Requiring that this equals 1 means that there is exactly 1 match.

Work with strings and use variables with descriptive names, it makes it easier to read what the program does. Comments inline.

``````from random import randint
firstnumber=randint (1,9)
print(firstnumber)
secondnumber=randint (1,9)
print(secondnumber)
a=  str(firstnumber) + str(secondnumber)
print (a)
# change to raw_input - numbers will be a string
numbers= raw_input("Enter 2 numbers")

# split the numbers up - 1st digit, second digit
lottery1, lottery2 = a, a
guess1, guess2 = numbers, numbers

# both digits, correct order
if lottery1 == guess1 and lottery2 == guess2:
print ("You have won 10000 dollars")
# both digits, reverse order
elif lottery1 == guess2 and lottery2 == guess1:
print ("You have won 3000 dollars")
# one digit
elif guess1 in (lottery1, lottery2)  or guess2 in (lottery1, lottery2):
print ("You have won 1000 dollars")
else:
print ('Try again')
``````

Top