我想使這個程序,我想從它隨機得到生成的列表中找到2,3,4,...,K,A,如果有多於一個編號n返回該號碼。同樣的第三高清,但我想要的C,D,H,S,然後返回它們有多少。但是我從第二場比賽中得到的所有結果都沒有。我需要修正什麼才能使其工作?Python中的列表數字字符
這裏是代碼,如果它可以幫助任何人
import random
def make_deck():
suits=['C', 'D', 'H', 'S']
ranks=['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
deck = [s+r for s in suits for r in ranks]
random.shuffle(deck)
return deck
def same_rank(hand, n):
if hand.count('2')>n:
count2=hand.count(2)
return count2
else:
return 'None'
if hand.count(3)>n:
count3=hand.count(3)
return count3
else:
return 'None'
if hand.count(4)>n:
count4=hand.count(4)
return count4
else:
None
if hand.count(5)>n:
count5=hand.count(5)
return count5
else:
return 'None'
if hand.count(6)>n:
count6=hand.count(6)
return count6
else:
return 'None'
if hand.count(7)>n:
count7=hand.count(7)
return count7
else:
return 'None'
if hand.count(8)>n:
count8=hand.count(8)
return count8
else:
return 'None'
if hand.count(9)>n:
count9=hand.count(9)
return count9
else:
return 'None'
if hand.count(10)>n:
count10=hand.count(10)
return count10
else:
return 'None'
if hand.count('J')>n:
countJ=hand.count('J')
return countJ
else:
return 'None'
if hand.count('J')>n:
countQ=hand.count('Q')
return countQ
else:
return 'None'
if hand.count('K')>n:
countK=hand.count('K')
return countK
else:
return 'None'
if hand.count('A')>n:
countA=hand.count('A')
return countA
else:
return 'None'
def same_suit(hand):
if hand.count('C')>0:
countC=hand.count('C')
if hand.count('D')>0:
countD=hand.count('D')
if hand.count('H')>0:
countH=hand.count('H')
if hand.count('S')>0:
countS=hand.count('S')
return countC, countD, countH, countS
hand = make_deck()[:10]
print hand
n=raw_input('Give n')
print same_rank(hand,n)
我把所有的人,如果因爲我想檢查所有的人,我知道,如果他們中的一個是事實,就不會再繼續檢查,如果別人的真實,是有機會的一個下一個也是如此。 – RedRose23
@ RedRose23問題不在於一旦一個成立就停止檢查其他人。無論如何,它在第一次檢查後停止檢查!無論'hand.count('2')> n'由於第一個'else'情況是真還是假,函數都會返回'return'。 – turbulencetoo
我做了你說的改動,加上我改進了代碼,所以現在就像你說的只是如果因爲我不能讓它檢查所有與elif(我沒有得到任何錯誤),現在唯一的問題是它最終不會返回,我不希望那樣。有什麼辦法可以刪除它嗎? (我把它放在前面,我也刪除它,但它是一樣的,即使我擦除其他東西仍然返回無) – RedRose23