我想让它通过函数洗牌来打印一张随机卡例如4颗红心这是我的当前代码,它一直在打印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((函数,而且您的函数不会返回任何内容,因此您的print语句将为空。
根据我在你的问题陈述中所能理解的,我想你可以简单地如下所示:
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())