[英]Define function which gives random values in a given range in Python
I am working on a blackjack game in python with no graphics
or pygame
, but I need it so if they use the hit
option it should give them a card with totally other value.我正在 python 中进行二十一点游戏,没有
graphics
或pygame
,但我需要它,所以如果他们使用hit
选项,它应该给他们一张完全其他价值的卡。 I have looked at In python, is there anyway to have a variable be a different random number everytime?我看过在 python 中,是否有一个变量每次都是不同的随机数? , and also many other websites.
,以及许多其他网站。 I have got no information about how to do this.
我没有关于如何做到这一点的信息。 Code:D
代码:D
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,'Queen':10, 'King':10, 'Ace':11}
###Printing Random
s = lambda: secure_choice.choice(suits)
r = lambda: secure_choice.choice(ranks)
ds = secure_choice.choice(suits)
dr = secure_choice.choice(ranks)
v = values.get(r)
dv = values.get(dr)
def pullCard():
print(f"You pulled a {r} of {s} the value is {v}")
print(f"The dealer pulled a {dr} of {ds} the value is {dv}")
def dealerOnly():
print('You standed')
pullCard()
Answer = str(input('Do you want to Hit or Stand?')).lower()
if Answer == 'stand':
dealerOnly()
elif Answer == 'hit':
pullCard()
What I think could work:我认为可行的方法:
def ReValueCard():
s = secure_choice.choice(suits)
r = secure_choice.choice(ranks)
I think the answer you are looking for is to do this and change the variables with what need to match.我认为您正在寻找的答案是这样做并更改需要匹配的变量。 Use the python built-in import
random
for this, and use the defined command, random.randint
to find your random card为此使用内置的 python 导入
random
数,并使用定义的命令random.randint
找到您的随机卡
import random
oldcard = 7
# the old card would be updated with every last old card
while randomcard == oldcard:
randomcard = random.randint(2, 11)
else:
print(randomcard)
hope that this helps!希望这会有所帮助!
if you just need it to be a random card everytime, and not a different random card, you can just do this:如果您只需要它每次都是随机卡,而不是不同的随机卡,您可以这样做:
import random
randomcard = random.randint(2, 11)
print(randomcard)
if you want a random suit too, just apply the random.randint
command for 1-4 and then just define each one as what suit如果你也想要一套随机套装,只需对 1-4 应用
random.randint
命令,然后将每个套装定义为什么套装
:D :D
Individual random number generation with stuff like random.randint
/ random.choice
/ random.randrange
has issues with avoiding repeats (you don't want to deal five Aces from a single deck after all).使用诸如
random.randint
/ random.choice
/ random.randrange
之类的东西生成单独的随机数存在避免重复的问题(毕竟你不想从一副牌中处理五个 A)。 The simplest solution is to actually make a deck of all the values, shuffle it, and deal from it in order, just like in the real world.最简单的解决方案是实际制作一个包含所有值的牌组,将其洗牌,然后按顺序处理,就像在现实世界中一样。
To make your deck, you just take the product
of the suits and ranks:要制作你的套牌,你只需要花色和等级的
product
:
import itertools
import random
suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')
ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
deck = list(itertools.product(suits, ranks))
then you shuffle it with the rather obviously named random.shuffle
:然后你用相当明显的名字
random.shuffle
:
random.shuffle(deck) # Shuffles in place
Now, to deal from the deck, just pop off cards on demand:现在,要从套牌中发牌,只需按需弹出卡片:
randomcard = deck.pop()
If they choose to hit again, just call deck.pop()
again to get a random, but non-repeating, card.如果他们选择再次击中,只需再次调用
deck.pop()
即可获得一张随机但不重复的卡片。 If you want to deal from multiple decks to make card counting harder, just multiply your deck before shuffling:如果您想从多副牌中发牌以使算牌更加困难,只需在洗牌前将您的牌相乘:
deck = list(itertools.product(suits, ranks)) * 5 # Deal from five decks, not 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.