使用randint函数并检查输入

时间:2013-10-11 12:56:32

标签: python

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个随机数并将它们组合在一起以创建彩票号码。这2个数字不应该相同,如果这2个数字相同,程序应该生成另外2个数字来制作新的彩票号码。该程序不应打印随机数,因为这是彩票中奖号码。

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

我基本上想知道如何修复我的程序(当输入是其他任何东西,然后输入赢得他们$ 10,000程序失败,因为他们的粗体线是错的。我也想知道如何生成另一个如果2个数字randint生成的彩票中奖号码是相同的,我也想知道如何检查输入以查看输入是否包含其中一个彩票号码。

2 个答案:

答案 0 :(得分:2)

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

  • 在你的长期条件下

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

    应该是

    int(b[1])== int(a[1])
    
  • 以前应该获得3000美元的胜利条件 赢得1000美元的条件。因为条件 当该人符合资格时,赢得1000美元也是真的 赢了3000美元。
  • 赢得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'}

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

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))计算b中的a中的字符数(按任意顺序)。要求这等于1意味着恰好有1个匹配。

答案 1 :(得分: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')