0
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')

该程序应该确定 2 个随机数并将它们放在一起以创建彩票号码。这两个号码不应该相同,如果这两个号码相同,程序应该生成另外两个号码来制作新的彩票号码。程序不应打印随机数,因为这是彩票中奖号码。

如果彩票号码是 53,奖励系统就是这样运作的。如果这个人按顺序猜对了号码(即 53),那么这个人应该赢得 10000 美元,如果这个人按相反的顺序(即 35 个)猜对了号码,他们应该赢得 3000 美元,如果他们猜对了其中一个数字(即 37 或 63 或 59 或 45),他们应该赢得 1000 美元。否则程序应该打印出来(再试一次)。

我基本上想知道如何修复我的程序(当输入不是赢得他们 10,000 美元的输入时,程序失败,因为他们的粗体线有问题。我还想知道如何生成另一个彩票中奖号码如果 randint 生成的 2 个数字相同,我还想知道如何检查输入以查看输入是否包含彩票号码之一。

4

2 回答 2

2

有许多事情可以改进,但一些明确的问题包括:

  • 在你的长期条件下

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

    应该

    int(b[1])== int(a[1])
    
  • 赢得3000美元的条件应该在赢得1000美元的条件之前。因为当这个人有资格赢得 3000 美元时,赢得 1000 美元的条件也为真。
  • 赢得 3000 美元的条件应该是

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

    (注意最后一个索引的变化),而不是

    int(b[1])==int(a[0]) and int(b[0])==int(a[0])
    
  • 由于您确实将响应视为字符串而不是整数,因此您不妨保留ab作为字符串。这样做意味着您可以删除所有对int.

因此,您的程序的工作版本将是:

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')

表达条件的一种更简单的方法是:

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')

关于len(set(b).intersection(a)) == 1:何时b是字符串,set(b)是字符串中的字符集:

In [62]: b = '75'

In [63]: set(b)
Out[63]: {'5', '7'}

集合有一个交集方法,它可以接受一个可迭代的作为输入。它返回原始集合中的项目集合,这些项目也在可迭代对象中。例如,

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()

所以计算其中len(set(b).intersection(a))的字符数(以任何顺序)。要求 this 等于 1 意味着恰好有 1 个匹配项。ba

于 2013-10-11T13:03:21.397 回答
0

使用字符串并使用具有描述性名称的变量,这样可以更容易地阅读程序的功能。内联评论。

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')
于 2013-10-11T13:45:40.297 回答