473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Blackjack game error

Im a novice in programming and i have these codes for a blackjack game.but it failed
Expand|Select|Wrap|Line Numbers
  1. from random import *
  2. from math import *
  3.  
  4. #GLOBAL VARIABLES
  5.  
  6. cards = range(0,52)
  7.  
  8. def randRange(in_lower,in_upper):
  9.     """ generates a random number between in_lower and in_upper"""
  10.     temp_range = in_upper - in_lower
  11.     return int(round((temp_range)*random() + (in_lower)))
  12.  
  13.  
  14. def popRandArray(in_list):
  15.     return in_list.pop(randRange(0,len(in_list)-1))
  16.  
  17. def realDealCard():
  18.  
  19.     global cards
  20.     if len(cards)==0:
  21.         print "new deck"
  22.         cards = range(0,52)
  23.     return popRandArray(cards)
  24.  
  25.  
  26. def cardAsString(in_card):
  27.  
  28.     value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
  29.     suit = ["hearts","diamonds","spades","clubs"]
  30.     return value[in_card%13]+ " of " + suit[in_card/13]
  31.  
  32.  
  33.  
  34. def cardScore(in_card):
  35.  
  36.     score = in_card%13+1
  37.     if score > 10:
  38.         score = 10
  39.     return score
  40. from random import *
  41. from math import *
  42.  
  43. #GLOBAL VARIABLES
  44.  
  45. cards = range(0,52)
  46.  
  47. def randRange(lower,upper):
  48.     """return a value in the range in_lower to in_upper
  49.     inclusive
  50.     """
  51.     temp_range = upper - lower
  52.     return int(round((temp_range+0.5)*random() + (lower - 0.5)))
  53.  
  54. def dealCard():
  55.     """simulate a deal from a hand of cards
  56.     """
  57.     return randRange(0,51)
  58.  
  59. def randArray(in_list):
  60.     return in_list[randRange(0,len(in_list)-1)]
  61.  
  62. def popRandArray(in_list):
  63.     """ return a random value from an array with no replacement
  64.     """
  65.     return in_list.pop(randRange(0,len(in_list)-1))
  66.  
  67. def dealCardTuple():
  68.     """return a tuple with a random card assigned
  69.     """
  70.     return int(randRange(0,12)),int(randRange(0,3))
  71.  
  72. def cardTupleAsString(in_tuple):
  73.     """turn a deal from a hand of cards as a tuple into a string
  74.     """
  75.     temp_value,temp_suite = in_tuple 
  76.     value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
  77.     suite = ["hearts","diamonds","spades","clubs"]
  78.     return value[temp_value]+ " of " + suite[temp_suite]
  79.  
  80. def cardAsString(in_card):
  81.     """turn a deal from a hand of cards as a tuple into a string
  82.     """
  83.     value = ["ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"]
  84.     suit = ["hearts","diamonds","spades","clubs"]
  85.     return value[in_card%13]+ " of " + suit[in_card/13]
  86.  
  87. def realDealCard():
  88.     """performs a non replacement randomised lookup
  89.     """
  90.     global cards
  91.     #check to see if the array still has a card in it
  92.     if len(cards)==0:
  93.         print "new deck"
  94.         cards = range(0,52)
  95.     return popRandArray(cards)
  96.  
  97. def testCards():
  98.     """tests the distribution of card when dealing a mulitple
  99.     number of hands"""
  100.     hands=int(raw_input('how many hands do you want to play?:'))
  101.     histo = 52 * [0]
  102.     for i in range(1,hands*52+1):
  103.         n = realDealCard()
  104.         histo[n] = histo[n] + 1
  105.     for i,v in enumerate(histo):
  106.         print cardAsString(i) + " " + str(v)
  107. #print dealCard()
  108. def cardScore(in_card):
  109.     """converts a card into a numerical score"""
  110.     score = in_card%13+1
  111.     if score > 10:
  112.         score = 10
  113.     return score
  114.  
  115. def bestScore(in_array):
  116.     """take an array and produces the best possible score without
  117.     going bust"""
  118.     #what is total score with ace = 1
  119.     score_array = map(cardScore,in_array)
  120.     print score_array
  121.     #what's the difference between 21 and the score
  122.     #if it's >= 10 then add on 10
  123.     out_score = sum(score_array)
  124.     if 1 in score_array:
  125.         if 21-out_score >=10:
  126.             return out_score+10
  127.         else:
  128.             return out_score
  129.     else:
  130.         return out_score
  131.  
  132. def exampleCards():
  133.     for i in range(100):
  134.         player_hand =    [realDealCard() for i in range(3)]
  135.         print player_hand
  136.         score = bestScore(player_hand)
  137.         if score>21:
  138.             print "bust"
  139.         else:
  140.             print score
  141.  
  142. exampleCards()
Apr 21 '10 #1
1 1645
bvdet
2,851 Expert Mod 2GB
nytgangsta101,

I renamed the thread to a more appropriate title. I also added code tags for you. Please see posting guidelines here.

If you would post the actual error you received including the traceback, it would help us pinpoint the problem.

BV - Moderator
Apr 21 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
3
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...
1
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...
1
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...
1
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()...
3
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,...
7
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.*;...
2
by: Z E R O | last post by:
I'm in a really fast pace training program and we are covering a lot of material in a relatively short period of time. I've really not had much trouble up until this problem. I had to do one other...
30
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...
21
by: imran akhtar | last post by:
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 = *4...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.