hi, i am making black jack code, but i am stuck, i have made start, but for reason gettin errors, which i dont seem be able to fix, below is my code wht i started. - import random
-
-
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,11]*4
-
player=[]
-
computer=[]
-
#r = random.randint
-
-
computer.append(random.randint(deck))
-
player.append(random.randint(deck))
-
computer.append(random.randint(deck))
-
print " The computer hand %d " % computer
-
player.append(random.randint(deck))
-
print " The player hand %d" % player
-
the error which i am getting is:
Traceback (most recent call last):
File "C:\Users\imran\Desktop\jack.py", line 31, in ?
computer.append(random.randint(deck))
TypeError: randint() takes exactly 3 arguments (2 given)
21 4147
random.randint takes two numbers as arguments and returns a random number that's between them. I think what you're looking for is random.choice, which selects a random item from a list: - computer.append(random.choice(deck))
Another possibility is to actually shuffle the deck with random.shuffle before dealing cards: - random.shuffle(deck)
-
computer.append(deck[0])
-
player.append(deck[1])
-
computer.append(deck[2])
Hope this helps.
ok thanks no i dont have no erros,, but how would i display the players hand, computers hand.
below is how the code looks now, but, would have to keep the code commeted out, as that think would display the, hands.
thanks for help - import random
-
-
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10,11]*4
-
player=[]
-
computer=[]
-
random.shuffle(deck)
-
computer.append(deck[0])
-
player.append(deck[1])
-
computer.append(deck[2])
-
-
##computer.append(random.choice(deck))
-
##player.append(random.choice(deck))
-
##
-
##computer.append(random.choice(deck))
-
##print " The computer hand %d " % computer
-
##
-
##player.append(random.choice(deck))
-
##print " The player hand %d" % player
thanks for ur help before "boxfish" , i was able, proceed and pretty much complete my blackjack code. however, i have come across a minor problem, my hit function does not seem to work. do you know how i can fix this. also, does anyone know how i can fit a re-run code into my program too? - import random
-
-
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
-
player=[]
-
computer=[]
-
random.shuffle(deck)
-
player.append(deck[0])
-
player.append(deck[1])
-
computer.append(deck[2])
-
computer.append(deck[3])
-
playerstotal = player [0]
-
playerstotal2 = player [1]
-
ptotal = playerstotal + playerstotal2
-
comptotal = computer [0]
-
comptotal2 = computer [1]
-
ctotal = comptotal + comptotal2
-
print "players hand", player
-
print ptotal
-
print "computers hand", computer
-
print ctotal
-
-
def game():
-
options = raw_input ("do you wana [h]it / [s]tand / [q]uit: ")
-
while options == 'h':
-
player.append
-
print ptotal
-
break
-
while options == 's':
-
print "players hand", player
-
print ptotal
-
break
-
-
if ctotal < 16:
-
computer.append
-
-
if ptotal > 21:
-
print "player busted. computer wins"
-
if ctotal > 21:
-
print "computer busted. player wins"
-
if ctotal == ptotal:
-
print "draw. computer wins"
-
if ptotal == 21:
-
print "player gets a BlackJack. player wins"
-
if ctotal == 21:
-
print "computer gets a BlackJack. computer wins"
-
if ptotal > ctotal:
-
if ptotal < 21:
-
print "player wins"
-
if ctotal > ptotal:
-
if ctotal < 21:
-
print "computer wins"
-
-
while options == 'q':
-
print "hope you enjoyed the game, bye!!"
-
break
-
-
game()
-
-
thanks, appreciate the help.
The line in your hit function,
player.append
does nothing. You need to specify something to append, which means you need a way of keeping track of which card must be dealt next. One way to do this is to delete cards from the deck as you deal them, like this, - player.append(deck[0])
-
del deck[0]
-
player.append(deck[0]) # When you delete the first card, deck[1] becomes deck[0]
-
del deck[0]
-
computer.append(deck[0])
-
del deck[0]
-
computer.append(deck[0])
-
del deck[0]
so that the next card to be dealt is always deck[0].
Another possibility is to store the index of the next card to be dealt in a variable: -
nextCard = 0
-
player.append(deck[nextCard])
-
nextCard += 1
-
player.append(deck[nextCard]) # nextCard is now 1.
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
I think you need to calculate ptotal every time the player gets a new card and calculate ctotal every time the computer gets a new card. Calculating them just once at the beginning of the game won't work.
I hope this is helpful, although your program will still need more work after you fix these problems.
i have tried the second option, but does not seem to work. -
import random
-
-
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
-
player=[]
-
computer=[]
-
random.shuffle(deck) #this will shuffle the cards in the deck
-
nextCard = 0
-
player.append(deck[nextCard])
-
nextCard += 1
-
player.append(deck[nextCard]) # nextCard is now 1.
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
-
playerstotal = player [0]
-
playerstotal2 = player [1]
-
ptotal = playerstotal + playerstotal2 #this (ptotal) is the players overall total
-
comptotal = computer [0]
-
comptotal2 = computer [1]
-
ctotal = comptotal + comptotal2 #this (ctotal) is the computers overall total
-
print "players hand", player, "\tplayers total value", ptotal #this will display the players hand
-
-
#hit or stand,are players options.
-
def game():
-
options = raw_input ("do you wana [h]it / [s]tand / [q]uit: ")
-
while options == 'h':
-
player.append(deck[nextCard])
-
nextCard += 1
-
print ptotal
-
break
-
while options == 's':
-
print "players hand", player
-
print ptotal
-
break
-
-
if ctotal < 16:
-
computer.append(deck[nextCard])
-
nextCard += 1
-
if ptotal > 21:
-
print "player busted. computer wins"
-
if ctotal > 21:
-
print "computer busted. player wins"
-
if ctotal == ptotal:
-
print "draw. computer wins"
-
if ptotal == 21:
-
print "player gets a BlackJack. player wins"
-
if ctotal == 21:
-
print "computer gets a BlackJack. computer wins"
-
if ptotal > ctotal:
-
if ptotal < 21:
-
print "player wins"
-
if ctotal > ptotal:
-
if ctotal < 21:
-
print "computer wins"
-
-
while options == 'q':
-
print "hope you enjoyed the game, bye!!"
-
break
-
-
game()
bvdet 2,851
Expert Mod 2GB
You can deal cards from a deck with one statement using random.choice() and list method pop().
Example: - >>> hand = []
-
>>> deck = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
-
>>> import random
-
>>> hand.append(deck.pop(random.choice(range(len(deck)))))
-
>>> hand
-
[10]
-
>>> deck
-
['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10]
Notice one of the 10's has been removed from the deck.
You can score aces as 1 or 11 using a function like this: - def hand_score(hand):
-
score = sum([card for card in hand if isinstance(card, int)])
-
aces = [card for card in hand if isinstance(card, str)]
-
score += len(aces)
-
for ace in aces:
-
if score < 12:
-
score += 10
-
return score
The function sums all the numbers and assigns the value to variable score. Then a 1 is added to score for each ace. A for loop adds 10 to score for each ace if the current value of score is less than 12. Example: - >>> hand_score([6,7,'A'])
-
14
-
>>> hand_score(['A', 'A', 'A', 3, 4])
-
20
-
>>> hand_score([10, 'A'])
-
21
-
>>> hand_score([10, 'A', 'A'])
-
12
-
>>>
HTH
thanks for your post, which may be helpful for other parts of my code, however my main problem is getting the "hit" function to work, as it is not working at the moment. have a look previous posts in this thread, and then could you be able to help me fix the "hit"?
thanks once again.
bvdet 2,851
Expert Mod 2GB
What error message did you receive? When I run your code, I get this error: UnboundLocalError: local variable 'nextCard' referenced before assignment
This should solve that problem: - def game():
-
global nextCard # I added this statement
-
while True:
-
options = raw_input ("do you wana [h]it / [s]tand / [q]uit: ")
Instead of using while statements, I suggest using an if/elif/else block for the user options. - if options == 'h':
-
player.append(deck[nextCard])
-
nextCard += 1
-
print ptotal
-
-
elif options == 's':
-
print "players hand", player
-
print ptotal
-
-
elif options == 'q':
-
print "hope you enjoyed the game, bye!!"
-
return
I added in what you told me to bvdet, and i get no errors, but when i 'hit', another card is not dealt and it still comes up with the previous total.
here is my code now: - import random
-
-
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
-
player=[]
-
computer=[]
-
random.shuffle(deck) #this will shuffle the cards in the deck
-
nextCard = 0
-
player.append(deck[nextCard])
-
nextCard += 1
-
player.append(deck[nextCard]) # nextCard is now 1.
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
-
playerstotal = player [0]
-
playerstotal2 = player [1]
-
ptotal = playerstotal + playerstotal2 #this (ptotal) is the players overall total
-
comptotal = computer [0]
-
comptotal2 = computer [1]
-
ctotal = comptotal + comptotal2 #this (ctotal) is the computers overall total
-
print "players hand", player, "\tplayers total value", ptotal #this will display the players hand
-
-
#hit or stand,are players options.
-
def game():
-
global nextCard
-
while (True):
-
options = raw_input ("do you wana [h]it / [s]tand / [q]uit: ")
-
if options == 'h':
-
player.append(deck[nextCard])
-
nextCard += 1
-
print ptotal
-
#break
-
elif options == 's':
-
print "players hand", player
-
print ptotal
-
#break
-
elif options == 'q':
-
print "hope you enjoyed the game, bye!!"
-
return
-
#break
-
-
if ctotal < 16:
-
computer.append(deck[nextCard])
-
nextCard += 1
-
if ptotal > 21:
-
print "player busted. computer wins"
-
if ctotal > 21:
-
print "computer busted. player wins"
-
if ctotal == ptotal:
-
print "draw. computer wins"
-
if ptotal == 21:
-
print "player gets a BlackJack. player wins"
-
if ctotal == 21:
-
print "computer gets a BlackJack. computer wins"
-
if ptotal > ctotal:
-
if ptotal < 21:
-
print "player wins"
-
if ctotal > ptotal:
-
if ctotal < 21:
-
print "computer wins"
-
-
game()
here is what happens when i run the game: - Y [yes] to view the rules , N [no]to start the game:n
-
start game
-
players hand [8, 4] players total value 12
-
do you wana [h]it / [s]tand / [q]uit: h
-
12
-
computer wins
-
do you wana [h]it / [s]tand / [q]uit:
bvdet 2,851
Expert Mod 2GB
A "card" is appended to list object player, but you are not updating the player's score. If the initial two cards are [6,7], the score would be sum([6,7]) = 13. If the player's hand is hit with a 3, the score would be sum([6,7,3]) = 16.
ok so it is not updating because the total itself is not updating. do you know how i can change it so the total updates?
thanks.
bvdet 2,851
Expert Mod 2GB @imran akhtar
The interpreter will do whatever you tell it to do. I suggested earlier a function to calculate the total each time. You do not need variable nextCard. Use list method pop() to return a card and remove from deck. I moved the location where the computer's hand is updated. If computer busts, player wins and there is no need to hit player. I added string method lower() to options to allow an uppercase entry. The code is by no means finished, but you should be able to take it from here. -
import random
-
-
def calc_score(hand):
-
score = sum([card for card in hand if isinstance(card, int)])
-
aces = [card for card in hand if isinstance(card, str)]
-
score += len(aces)
-
for ace in aces:
-
if score < 12:
-
score += 10
-
return score
-
-
#hit or stand,are players options.
-
def game():
-
-
deck = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
-
player=[]
-
computer=[]
-
random.shuffle(deck) #this will shuffle the cards in the deck
-
-
# deck.pop() will return a card and remove from deck
-
player.append(deck.pop())
-
computer.append(deck.pop())
-
player.append(deck.pop())
-
computer.append(deck.pop())
-
-
player_score = calc_score(player)
-
computer_score = calc_score(computer)
-
-
#this will display the players hand
-
print "players hand", player, "\tplayers total value", player_score
-
-
# update computer hand until the score is >= 16 or busted
-
while computer_score < 16:
-
computer.append(deck.pop())
-
computer_score = calc_score(computer)
-
-
if computer_score > 21:
-
# CHECK IF COMPUTER IS BUSTED HERE
-
print "COMPUTER IS BUSTED!"
-
return
-
-
while True:
-
options = raw_input ("do you want to [h]it / [s]tand / [q]uit: ")
-
-
if options.lower() == 'h':
-
player.append(deck.pop())
-
print player
-
player_score = calc_score(player)
-
print player_score
-
# CHECK IF PLAYER IS BUSTED HERE
-
if player_score > 21:
-
print "PLAYER IS BUSTED!"
-
return
-
-
elif options.lower() == 's':
-
print "players hand", player
-
print player_score
-
# COMPARE SCORES HERE TO DETERMINE THE WINNER
-
-
elif options.lower() == 'q':
-
print "hope you enjoyed the game, bye!!"
-
return
-
game()
hey. i dont think you seem to understand. i've done my own code and would rather stick to how it is, i appreciate u writing ur own version of the code but i would rather stick with mine, and jus fix the 'hit' function. you say i can fix it by creating a function to calculate the total, how would i do that?
thanks.
bvdet 2,851
Expert Mod 2GB
Hey back. You don't understand that I have modified your code, not written my own. The improvements I proposed to you eliminated the awkward way you were calculating the initial score, updated the score after each hit, removed cards from the deck each time a card was dealt and eliminated the variable nextCard, eliminated the string of if statements that executed each iteration, and showed you how to count aces as 1 or 11.
If all you want to fix in your previous code is updating the score: - if options == 'h':
-
player.append(deck[nextCard])
-
ptotal = sum(player) # <---PLAYER SCORE UPDATED HERE
-
nextCard += 1
-
print ptotal
Good luck and HTH.
thnaks for ur help with updating the score, it now works, but i have two main problem, firstly how would i now get the ace to be 1 and 11, and a way of displaying the total hands played at the end of the game.
thanks for the help
below is my code, - import random
-
-
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
-
player=[]
-
computer=[]
-
random.shuffle(deck) #this will shuffle the cards in the deck
-
nextCard = 0
-
player.append(deck[nextCard])
-
nextCard += 1
-
player.append(deck[nextCard]) # nextCard is now 1.
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
-
computer.append(deck[nextCard])
-
nextCard += 1
-
ptotal = sum(player)
-
ctotal = sum(computer)
-
print "players hand", player, "\tplayers total value", ptotal #this will display the players hand
-
#hit or stand,are players options.
-
def game():
-
global nextCard
-
while (True):
-
options = raw_input ("do you wana [h]it / [s]tand / [q]uit: ")
-
if options == 'h':
-
player.append(deck[nextCard])
-
ptotal = sum(player)
-
nextCard += 1
-
print "players hand", player, "\tplayers total value", ptotal
-
-
elif options == 's':
-
ptotal = sum(player)
-
print "players hand", player, "\tplayers total value", ptotal
-
break
-
elif options == 'q':
-
playAgain = raw_input ("do u wana play again, [y] / [n]")
-
if playAgain == "y":
-
game()
-
elif playAgain == "n":
-
print "see ya later, bye!"
-
return
-
break
-
-
while (True):
-
if ctotal < 16:
-
computer.append(deck[nextCard])
-
nextCard += 1
-
if ptotal > 21:
-
print "computer hand", computer, "\tcomputers total value", ctotal
-
print "player busted. computer wins"
-
break
-
if ctotal > 21:
-
print "computer hand", computer, "\tcomputers total value", ctotal
-
print "computer busted. player wins"
-
break
-
if ctotal == ptotal:
-
print "computer hand", computer, "\tcomputers total value", ctotal
-
print "draw. computer wins"
-
break
-
if ptotal == 21:
-
print "computer hand", computer, "\tcomputers total value", ctotal
-
print "player gets a BlackJack. player wins"
-
break
-
if ctotal == 21:
-
print "computer hand", computer, "\tcomputers total value", ctotal
-
print "computer gets a BlackJack. computer wins"
-
break
-
if ptotal > ctotal:
-
if ptotal < 21:
-
print "computer hand", computer, "\tcomputers total value", ctotal
-
print "player wins"
-
break
-
if ctotal > ptotal:
-
if ctotal < 21:
-
print "computer hand", computer, "\tcomputers total value", ctotal
-
print "computer wins"
-
break
-
-
game()
-
-
-
-
bvdet 2,851
Expert Mod 2GB
I showed you how to count an ace as 1 or 11 already by defining a deck of cards with an "A" instead of a 1 and 11, and defining a function to determine the score of a hand. Here is another way, using a 1 instead of an "A". - >>> def sum_score(hand):
-
... score = sum(hand)
-
... for card in hand:
-
... if card == 1 and score < 12:
-
... score += 10
-
... return score
-
...
-
>>> deck = [1,2,3,4,5,6,7,8,9,10,10,10,10]*4
-
>>> import random
-
>>> random.shuffle(deck)
-
>>> hand1 = [deck.pop() for i in range(3)] # a hand with 3 cards
-
>>> hand2 = [deck.pop() for i in range(3)] # another hand with 3 cards
-
>>> hand1
-
[5, 10, 10]
-
>>> sum_score(hand1)
-
25
-
>>> hand2
-
[1, 4, 5]
-
>>> sum_score(hand2)
-
20
-
>>>
hand2 contains a 1 which represents an ace. Function sum_score() adds 10 to the score because the initial sum is less than 12.
-BV
yeh thanks, after few changes it is now wroking, is there a way of how i would display total hands played, at the end of game, for both computer and player
bvdet 2,851
Expert Mod 2GB
Your code is designed for one hand. You will need a way to deal additional hands, therefore another option is required. Create an empty list at the beginning of the script, and append each hand's results to it.
-BV
ok, a bit confused, dont understand how i would start it, thanks
so is it possiable of you can explain bit more, how i would display the hands.
thanks
bvdet 2,851
Expert Mod 2GB
Create an empty list names results, and append the result of each hand to the list after the winner of the hand is determined. When the user decides to quit playing, ite3rate on the list to display the all results. - results = []
-
results.append(['player',[1,5,5],[6,9,4]])
-
results.append(['computer',[6,5,6],[9,10]])
-
for result in results:
-
print "Winner: %s" % result[0]
-
print " Player hand: %s" % (', '.join([str(i) for i in result[1]]))
-
print " Computer hand: %s" % (', '.join([str(i) for i in result[2]]))
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Charlie Cosse |
last post by:
Asymptopia BlackJack is written in Python and uses PyGame for
multimedia.
Version 1.2 contains both Windows and Linux install scripts.
Asymptopia BlackJack is a full-featured casino-style...
|
by: slyphiad |
last post by:
Here's the problem that i got...
I'm trying to create a blackjack game. Here, I'm trying to create 2
blackjack games. A game with bet and without bet. So basically what i
did, was create 2...
|
by: CapMaster |
last post by:
I've found some programs of how to create a standard game of blackjack on C++. But how would you do it using structs? Here is my assignment:
Problem Statement: The purpose of this project is to...
|
by: mturner64 |
last post by:
Trying to link my Samsung BlackJack phone to my laptop to achieve internet access. When I plug the phone into the laptop the computer recognizes the BlackJack but I do not know how to configure one...
|
by: blinkrebel |
last post by:
Hello
I need some help implementing the game of blackjack using the xturtle package.
the instructions can be found at
http://katie.luther.edu/moodle/file.php/2387/BlackJack.pdf
and .gif's...
|
by: EXotiX |
last post by:
hey im new to vb6 and I am making a blackjack game but can not randomize the array correctly. Could you help please
Option Explicit
Const NumItems As Integer = 53
Public Sub RandomizeCards()...
|
by: devilinthebox |
last post by:
I am not really familar with Java and I need help with creating this simple Blackjack program.
Here is a layout of how the program should output:
If the computer has more than 16 it wins,...
|
by: devilinthebox |
last post by:
I'm fairly new to java and need help with adding letters (J, Q, K, A) into the program and adding values for each. Thanks.
// February 8, 2008
// The "BlackJack" class.
import java.awt.*;...
|
by: imran akhtar |
last post by:
i have a balckjack code, which does not seem to run, in python, it comes up with syntax error, i have try sortng it out. does not seem to work. below is my code, if anyone can work out wht wrong...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |