Question:
Python Programming Help?
anonymous
2010-04-20 09:10:28 UTC
Hi there;

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
Three answers:
merpius
2010-04-20 13:49:50 UTC
Ok, as the previous answerer said; you need to pass in a value for draw (probably the one you just got from drawing the card); card = nameCard() should be card = nameCard(draw)



Additionally, at the start of your main() function you do not need to do the following:

module2.nameCard(draw)

module2.suit()

module2.dealCard()



since those are calling the functions.

In fact, given the way you are trying to call the functions, it would probably be best for you to import them into your main namespace;

use:

from module2 import *

instead of

import module2
TheMadProfessor
2010-04-20 12:18:10 UTC
I'm not familiar with Python, but the error seems fairly indicative...both the prototype and definition of function nameCard has draw as a passed parameter, but when you invoke it in main, you don't pass it a value. Try changing the line to



card = nameCard(draw)
delores
2016-06-02 12:35:50 UTC
If you have read anything about sound in "Console-Mode" Python, that's because there is no sound feature with Console Python. You need to learn wxPython or PyGame or maybe even Tkinter (I think supports sound features)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...