Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 18th, 2008, 04:24 AM
faria faria is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 13 faria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 19 sec
Reputation Power: 0
Blackjack code

here i have black jack code, which does not seem to run, as it keeps displaying syntax error. i dont seem to understand the problem and how to solve it, and was hoping if anyone can help me out, below is the code, if any one can copy it in to python, and see y it does not run. thankyou

Code:
from random import choice as randomcards
     
def total(hand):
        # how many aces in the hand
        aces = hand.count(11)
        # to complicate things a little the ace can be 11 or 1
        # this little while loop figures it out for you
        t = sum(hand)
        # you have gone over 21 but there is an ace
        if t > 21 and aces > 0:
            while aces > 0 and t > 21:
                # this will switch the ace from 11 to 1
                t -= 10
                aces -= 1
        return t
# a suit of cards in blackjack assume the following values
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
     
    # there are 4 suits per deck and usually several decks
    # this way you can assume the cards list to be an unlimited pool
     
cwin = 0  # computer win counter
pwin = 0  # player win counter
while True:
        player = []
        # draw 2 cards for the player to start
        player.append(rc(cards))
        player.append(rc(cards))
        pbust = False  # player busted flag
        cbust = False  # computer busted flag
while True:
            # loop for the player's play ...
        tp = total(player)
print "The player has these cards %s with a total value of %d" % (player, tp)

if tp > 21:
                print "--> The player is busted!"
                pbust = True
                break

else tp == 21:
                print "\a BLACKJACK!!!"
                break
            else:
                hs = raw_input("Hit or Stand/Done (h or s): ").lower()
                if 'h' in hs:
                    player.append(rc(cards))
                else:
                    break
        while True:
            # loop for the computer's play ...
           comp = []
            comp.append(rc(cards))
            comp.append(rc(cards))
            # dealer generally stands around 17 or 18
            while True:
                tc = total(comp)                
                if tc < 18:
                    comp.append(rc(cards))
                else:
                    break
            print "the computer has %s for a total of %d" % (comp, tc)
            # now figure out who won ...
            if tc > 21:
                print "--> The computer is busted!"
                cbust = True
                if pbust == False:
                    print "The player wins!"
                    pwin += 1
            elif tc > tp:
               print "The computer wins!"
                cwin += 1
            elif tc == tp:
                print "It's a draw!"
            elif tp > tc:
                if pbust == False:
                    print "The player wins!"
                    pwin += 1
                elif cbust == False:
                    print "The computer wins!"
                    cwin += 1
            break
        print
        print "Wins, player = %d  computer = %d" % (pwin, cwin)
        exit = raw_input("Press Enter (q to quit): ").lower()
        if 'q' in exit:
            break
        print

    print "Thanks for playing blackjack with the computer!"

Reply With Quote
  #2  
Old December 18th, 2008, 05:00 AM
c-2501 c-2501 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Scotland
Posts: 68 c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 29 m 49 sec
Reputation Power: 7
please use code tags when posting code examples as indentation is important in python, or better yet use [ highlight = "python" ] your code here[ / highlight ] without the spaces in the []

Reply With Quote
  #3  
Old December 18th, 2008, 05:11 AM
faria faria is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 13 faria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 19 sec
Reputation Power: 0
sorry for that, i have now edited, the message, and have put the code, in code tags.

Reply With Quote
  #4  
Old December 18th, 2008, 05:53 AM
c-2501 c-2501 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Scotland
Posts: 68 c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 29 m 49 sec
Reputation Power: 7
Your problem is here

Code:
else tp == 21:


else doesn't take in conditional in this way because it is designed to be run if all previous conditionals fail (i hope that makes sense).

You'll also want to fix the indentation after this as its kinda all over the place

Last edited by c-2501 : December 18th, 2008 at 05:55 AM.

Reply With Quote
  #5  
Old December 18th, 2008, 06:53 AM
faria faria is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 13 faria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 19 sec
Reputation Power: 0
ok understand that, how would i fix this, problem, for example how should it be instead, yuo have siad that there is a problem with "else tp == 21:" so how would i fix this.

Reply With Quote
  #6  
Old December 18th, 2008, 07:00 AM
c-2501 c-2501 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Scotland
Posts: 68 c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 29 m 49 sec
Reputation Power: 7
I would have thought that was fairly obvious as you have used multiple if statements elsewhere in the code.

Reply With Quote
  #7  
Old December 18th, 2008, 11:51 AM
faria faria is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 13 faria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 19 sec
Reputation Power: 0
i cant see the problem is. is possiable if u could show how i would correct it.
thankyou

Reply With Quote
  #8  
Old December 18th, 2008, 07:42 PM
c-2501 c-2501 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Location: Scotland
Posts: 68 c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level)c-2501 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 3 h 29 m 49 sec
Reputation Power: 7
I think perhaps you need to go back over the fundamentals of the language rather than jumping into this blackjack code you've come across, as basic conditional statements are at the very heart of how programs operate.

Reply With Quote
  #9  
Old December 19th, 2008, 11:22 AM
faria faria is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 13 faria User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 19 sec
Reputation Power: 0
can u please show me, how i would sort thsi problem out, with the "else tp == 21:" t

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Blackjack code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT