简体   繁体   中英

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. I have looked at In python, is there anyway to have a variable be a different random number everytime? , and also many other websites. I have got no information about how to do this. Code: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

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

: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). 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:

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(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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM