![](/img/trans.png)
[英]How would I go about getting this code to shuffle the deck two more times to complete the card trick?
[英]How can i shuffle the two arrays using functions so that it would print out a random card
我希望它通過洗牌來打印一張隨機卡片,例如 4 of heart 這是我當前的代碼,它一直打印 None of None
提前致謝
def exchange(SUITS, i, suits):
temp = SUITS[i]
SUITS[i] = SUITS[suits]
SUITS[suits] = temp
return SUITS[suits]
def shuffle():
SUITS = ['Clubs', 'Diamonds', 'Hearts' , 'Spades']
n = len(SUITS)
for i in range(n):
suits = random.randrange(0,n)
exchange(SUITS,i, suits)
def exchange2(RANKS, i, ranks):
Temp = RANKS[i]
RANKS[i] = RANKS[ranks]
RANKS[ranks] = Temp
return RANKS[ranks]
def shuffle2():
RANKS = ['2', ' 3', '4', '5', '6' , '7' , '8', '9','10',
'Jack', 'Queen', 'King','Ace']
n = len(RANKS)
for i in range(n):
ranks = random.randrange(0,n)
exchange2(RANKS,i, ranks)
w = shuffle()
z = shuffle2()
print(str(z) + ' of ' + str(w))
python 隨機庫有一個shuffle() function 用於列出您的 function 沒有返回任何內容,因此您的打印語句將為空。
根據我在您的問題陳述中的理解,我想您可以簡單地執行以下操作:
import random
def shuffle():
SUITS = ['Clubs', 'Diamonds', 'Hearts' , 'Spades']
RANKS = ['2', ' 3', '4', '5', '6' , '7' , '8', '9','10',
'Jack', 'Queen', 'King','Ace']
randomSuit = random.randrange(0, len(SUITS))
randomRank = random.randrange(0, len(RANKS))
print(RANKS[randomRank] + " of " + SUITS[randomSuit])
shuffle()
讓我知道這是否是您所期望的。
如果你想從不同的函數中得到等級和花色,那么也可以稍微修改一下來實現。
import random
def shuffleRank():
RANKS = ['2', ' 3', '4', '5', '6' , '7' , '8', '9','10',
'Jack', 'Queen', 'King','Ace']
randomRank = random.randrange(0, len(RANKS))
return RANKS[randomRank]
def shuffleSuit():
SUITS = ['Clubs', 'Diamonds', 'Hearts' , 'Spades']
randomSuit = random.randrange(0, len(SUITS))
return SUITS[randomSuit]
print (shuffleRank() + " of " + shuffleSuit())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.