[Tutor] I need advice about which way to go.

Bob Gailer bgailer at sbcglobal.net
Wed Aug 3 21:03:25 CEST 2005


At 11:36 AM 8/3/2005, Nathan Pinno wrote:
>Here is the code then:

I'll throw in some suggestions. 1 - check for balanced parentheses. This 
has bit you before and you have several lines below with unbalanced 
parentheses. 2 - since Python indexes start at 0, take advantage of this. 
Use random.choice(range(13) and use the value to index the cards list.

>#This is code for a blackjack game.
>import random
>cash = 0
>new_cash = 100
>cards = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", 
>"Nine", "Ten", "Jack", "Queen", "King"}
>card_types = {"Diamond", "Heart", "Spade", "Club"}
>bet = 0
>playertotal = 0
>comp_total = 0
>
>def menu():
>     print "1. Bet and play."
>     print "2. Cash out and Exit"
>
>def option():
>     return int(raw_input("Menu choice: "))
>
>def card_choice():
>     return random.choice(range(1,14)

This will return one number. The statement below (a,b = card_choice()) 
expects a tuple of 2 numbers to be returned.

>  def types():
>     return random.choice(range(1,5)
>
>def player_cards():
>     print a," of ",t1
>     print b," of ",t2
>
>print "Blackjack"
>print "By Nathan Pinno"
>while 1:
>     menu()
>     choice = option()
>     if choice == 1:
>         bet = int(raw_input("How much do you want to bet: ")

Something is wrong with the indentation below. Assuming the if and else 
following the while are indented more, you have a BIG problem. What will 
happen if bet > new_cash? Endless loop printing the Sorry... forever

>             while 1:
>             if bet > new_cash:
>                   print "Sorry, you don't have that much cash! Your total 
> cash is: $",new_cash
>             else:
>                   break
>         a,b = card_choice()
>         t1,t2 = types()
>         if t1 == 1:
>             t1 = card_types[0]
>         elif t1 == 2:
>             t1 = cardtypes[1]
>         elif t1 == 3:
>             t1 = cardtypes[2]
>         else:
>             t1 = cardtypes[3]

Why not just use the random integer as an index? If you use range(13) these 
if/elifs become
             t1 = cardtypes[t1]

>         if a == 1:
>             a = cards[0]
>             playertotal = playertotal + 1
>         elif a == 2:
>             a = cards[1]
>             playertotal = playertotal + 2

Same thing here.
What are the functions of b, t2, and playertotal? Especially for those of 
us who don't know blackjack.

Does that help?

Bob Gailer
phone 510 978 4454  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050803/0349a176/attachment.htm


More information about the Tutor mailing list