问题描述:

`from random import randint`

firstnumber=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[1])==int(a[0]) and int(b[0])==int(a[0]):

print ("You have won 3000 dollars")

**elif int(b[0])== int(a[0]) or int(b[0])==int(a[1]) or int(b[1])==int(a[0]) or int(b[1])== int[1]:**

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:

In your long condition

`int(b[1])== int[1]`

should be

`int(b[1])== int(a[1])`

- 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[1])==int(a[0]) and int(b[0])==int(a[1])`

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

`int(b[1])==int(a[0]) and int(b[0])==int(a[0])`

- 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[1] == a[0] and b[0] == a[1]:
print ("You have won 3000 dollars")
elif (b[0] == a[0]
or b[0] == a[1]
or b[1] == a[0]
or b[1] == a[1]):
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 [62]: b = '75'
In [63]: set(b)
Out[63]: {'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 [64]: set('75').intersection('5')
Out[64]: {'5'}
In [65]: set('75').intersection('baloney')
Out[65]: set()
In [66]: set('75').intersection('57')
Out[66]: {'5', '7'}
In [67]: set('75').intersection([5]) # strings and ints are not equal
Out[67]: 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[0], a[1]
guess1, guess2 = numbers[0], numbers[1]
# apply your logic
# 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')
```

您可能感兴趣的文章：

- Iterating over the named and unnamed elements separately in an R list
- foreach - Iterating over two arrays simultaneously using for each loop in Java
- webforms - ASP.Net web forms with ServiceStack.Net
- excel - How to get the value of a range within a range
- multithreading - make a copy of dead thread
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial