2013-10-11 98 views
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個隨機數並將它們放在一起以創建彩票號碼。這兩個數字不應該相同,如果這兩個數字相同,程序應該生成另外兩個數字來創建一個新的彩票號碼。該程序不應該打印隨機數字,因爲這是彩票中獎號碼。使用randint函數和檢查輸入

如果彩票號碼是53,獎勵系統將如何工作。如果該人按順序猜測正確的號碼(即53),那麼如果該人以相反的順序猜出正確的號碼(即53),則該人應該贏得$ 10000。 35)他們應該贏得3000美元,如果他們猜對了其中一個數字(即37或63或59或45),他們應該贏得1000美元。否則,程序應該打印出來(再試一次)。

我基本上想知道如何解決我的程序(當輸入是其他的輸入,贏得他們$ 10,000的程序失敗,因爲他們是粗體行錯了,我也想知道如何產生如果2個數字randint生成另一個彩票中獎號碼是相同的,我也想知道如何檢查輸入,看看輸入是否包含其中一個彩票號碼

+1

除非你告訴我們,否則我們不知道你的程序有什麼問題。 –

+1

'b'是一個整數('b = int(numbers)'),所以'b [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'} 

集具有可以接受一個可迭代作爲輸入的交叉方法。它從原始集合中返回也在迭代中的項目集合。例如,

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意味着恰好有一個匹配。

+0

您能解釋一下您對程序所做的更改,以檢查用戶是否正確猜測了1位數 – user2821664

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