anonymous
2010-04-20 09:10:28 UTC
I'm trying to get my python program to run a program that simulates poker. I have 99.9% percent of it all worked out, but when I split it into a module, it wouldn't work. I'll include my code, just so it's easier to understand. I'm not asking for a "this is how you do it" answer; just a point in the right direction. Thanks!
import module2
def main():
module2.nameCard(draw)
module2.suit()
module2.dealCard()
A_points = 0
B_points = 0
while A_points < 21 and B_points < 21:
draw = dealCard()
card= nameCard()
A_points += draw
print("The first player drew a "+card+" of "+suit()+".")
draw = dealCard()
card = nameCard()
B_points += draw
print("The second player drew a "+card+" of "+suit()+".")
print("The first player has %d points and the second player has %d points.\n" %(A_points,B_points))
if A_points > 21 and B_points <= 21:
print("The second player wins. This time, anyway!")
elif A_points <= 21 and B_points > 21:
print("The first player wins. This time anyway!")
elif A_points > 21 and B_points > 21:
print("Everyone loses!")
else:
print("What happened here?")
main()
------------------------------
import random
def nameCard(draw): # Function for giving a string name to the card with parameters for draw
if draw == 1:
card = "Ace"
elif draw == 2:
card = "two"
elif draw == 3:
card = "three"
elif draw == 4:
card = "four"
elif draw == 5:
card = "five"
elif draw == 6:
card = "six"
elif draw == 7:
card = "seven"
elif draw == 8:
card = "eight"
elif draw == 9:
card = "nine"
elif draw == 10:
card = "ten"
elif draw == 11:
card = "Jack"
elif draw == 12:
card = "Queen"
elif draw == 13:
card = "King"
else:
card = "Error received."
return card
def suit():
suit = random.randint(1,4)
if suit == 1:
c = "hearts"
if suit == 2:
c = "diamonds"
if suit == 3:
c = "clubs"
if suit == 4:
c = "spades"
return c
def dealCard():
c = random.randint(1,13)
return c
-----------------------------------
Traceback (most recent call last):
File "/Users/alexandriaabenshon/Documents/example.py", line 29, in
main()
File "/Users/alexandriaabenshon/Documents/example.py", line 4, in main
module2.nameCard(draw)
UnboundLocalError: local variable 'draw' referenced before assignment