Okay, I am writing a dopewars clone/wannabe in python. I have it pretty much the way I want it, except that selling items does not work. Buying them does fine, it is just selling them.

Here is the code:

#!/usr/bin/env python
#
#       pimp.py
#       
#       @authors: KarimRuan, 
#       Copyright 2009 
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.
import sys, os
import random

global cash
cash = 500
global dealers
dealers = 2
global hp
hp = 25
global gang
gang = 1
global guns
guns = 0

global thugHP
thugHP = 20


def la_prompt():
	print """COMING SOON"""
	return

def la():
	clear()
	global cash, dealers, hp, gang
	print "CASH: ", cash,  "dealers: ", dealers, "HP: ", hp, "GANG MEMBERS: ", gang
	print
	print 'you are in Los Angelos.'
	print 
	print 'Rictor sells GUNS. Use "trade guns". Ace sells DEALERS. use "buy DEALERS".'
	print 'You should tell your dealers "dealers work".'
	print "You can also go : cleveland, bronxe, NY"
	i = random.randrange(1, 5)
	if i == 1:
		print "2 thugs want to join your gang!"
		gang = gang + 2
		la_prompt()
		
	elif i == 2:
		fight()
		la_prompt()
	elif i == 3:
		print 'You find $500'
		cash = cash + 500
		la_prompt()
	elif i == 4:
		cop_fight()
		la_prompt() 
	elif i == 5:
		print 'Two dealers want to join your crew.'
		dealers = dealers + 2
		la_prompt()
	else:
		print "Error."
		la_prompt()
	la_prompt()




def foo_sell():
	global cash, dealers
	clear()
	print """\
	
	Sell how many DEALERS?
	
	a. 1 dealer for $50
	b. 2 dealers for $100
	c. 3 dealers for $150
	
	or:
	
	d. exit
	"""
	x = raw_input("> ")
	if x == 'a':
		if dealers > 1:
			print "You sell 1 dealer  for $50."
			dealers = dealers - 1
			cash = cash + 50
			foo_sell()
		else:
			print "You do not have enough dealers to sell."
			print 'You have ', dealers, ' dealers to sell.'
			foo_sell()
	elif x == 'b':
		if dealers > 2:
			print 'You sell 2 dealers for $100.'
			dealers = dealers - 2
			cash = cash + 100
			foo_sell()
		else:
			print "You do not have enough to sell 2 dealers."
			print "You have ", dealers, " dealers to sell."
			foo_sell()
	elif x == 'c':
		if dealers > 3:
			print 'You sell 3 dealers for $150.'
			dealers = dealers - 3
			cash = cash + 150
			foo_sell()
		else:
			print 'You do not have enough to sell 3 dealers.'
			print 'You have ', dealers, ' dealers to sell.'
			bob_sell()
	elif x == 'd':
		cleveland()	
	
	
	
def foo_buy():
	global cash, dealers
	print """\
	
		a. buy 1 guns for $100
		b. buy 2 dealers for $200
		c. buy 3 dealers for $300
		d. exit
		"""
	x = raw_input("> ")
	if x == 'a':
		if cash < 100:
			print "Not enough cash."
			foo_buy()
		else:
			dealers = dealers + 1
			cash = cash - 100
			foo_buy()
	elif x == 'b':
		if cash < 200:
			print "Not enough cash."
			foo_buy()
		else:
			dealers = dealers + 2
			cash = cash - 200
			foo_buy()
	elif x == 'c':
		if cash < 300:
			print "Not enough cash."
			foo_buy()
		else:
			dealers = dealers + 3
			cash = cash - 300
			foo_buy()
	elif x == 'd':
		cleveland()
			
	
def foo_prompt():
	x = raw_input("> ")
	if x == 'buy dealers':
		foo_buy()
	elif x == 'sell dealers':
		foo_sell()
	elif x == 'exit':
		cleveland()
	else:
		print "Not Recognized."
		foo_prompt()
		

def foo_trade():
	clear()
	global cash, dealers
	print "YOUR CASH: ", cash
	print "YOUR dealers: ", dealers
	print
	print """\
	
	YO YO YO, I gots some mad hustlin' dealers for sale. Jus use
	'buy dealers'. I can also take some of them dealers off ya hands.
	Jus use 'sell dealers'. Use 'exit' to leave my shop.
	"""
	foo_prompt()
	

################################################
#  BOB TRADING
################################################
def bob_sell():
	global cash, guns
	clear()
	print """\
	
	Sell how many GUNS?
	
	a. 5 guns for $35
	b. 10 guns for $75
	c. 20 guns for $175
	
	or:
	
	d. exit
	"""
	x = raw_input("> ")
	if x == 'a':
		if guns > 5:
			print "You sell 5 guns for $35."
			guns = guns - 5
			cash = cash + 35
			bob_sell()
		else:
			print "You do not have enough guns to sell."
			print 'You have ', guns, 'to sell.'
			bob_sell()
	elif x == 'b':
		if guns > 10:
			print 'You sell 10 guns for $75.'
			guns = guns - 10
			cash = cash + 75
			bob_sell()
		else:
			print "You do not have enough to sell 10 guns."
			print "You have ", guns, " to sell."
			bob_sell()
	elif x == 'c':
		if guns > 20:
			print 'You sell 20 guns for $175.'
			guns = guns - 20
			cash = cash + 175
			bob_sell()
		else:
			print 'You do not have enough to sell 20 guns.'
			print 'You have ', guns, ' to sell.'
			bob_sell()
	elif x == 'd':
		cleveland()	
	
	
	
def bob_buy():
	global cash, guns
	print """\
	
		a. buy 5 guns for $50
		b. buy 10 guns for $100
		c. buy 20 guns for $200
		d. exit
		"""
	x = raw_input("> ")
	if x == 'a':
		if cash < 50:
			print "Not enough cash."
			bob_buy()
		else:
			guns = guns + 5
			cash = cash - 50
			bob_buy()
	elif x == 'b':
		if cash < 100:
			print "Not enough cash."
			bob_buy()
		else:
			guns = guns + 10
			cash = cash - 100
			bob_buy()
	elif x == 'c':
		if cash < 200:
			print "Not enough cash."
			bob_buy()
		else:
			guns = guns + 20
			cash = cash - 200
			bob_buy()
	elif x == 'd':
		cleveland()
			
	


def bob_prompt():
	x = raw_input("> ")
	if x == 'buy guns':
		bob_buy()
	elif x == 'sell guns':
		bob_sell()
	elif x == 'exit':
		cleveland()
	else:
		print "Not Recognized."
		bob_prompt()
		

def bob_trade():
	clear()
	global cash, guns
	print "YOUR CASH: ", cash
	print "YOUR GUNS: ", guns
	print
	print """\
	
	HI: my name is Bob. Do you want to 'sell guns' or 'buy guns'?
	type 'exit' to exit shop.
	"""
	bob_prompt()
	
def work_dealers():
	global cash
	cearn = random.randrange(50, 99)
	print "you earned ", cearn, "cash."
	cash = cash + cearn
	work_dealers

'''
def work_dealers():
	global cash
	income = random.randint(self, 1, 9)
	cash = cash + income
	work_dealers()
'''
def clev_income():
	work_dealers()

def clev_prompt():
	global cash, dealers, hp, gang, thugHP
	x = raw_input("> ")
	if x == 'dealers work':
		clev_income()
		clev_prompt()
	elif x == 'trade guns':
		bob_trade()
		clev_prompt()
	elif x == 'buy dealers':
		foo_trade()
		clev_prompt()
	elif x == 'go la':
		la()
	elif x == 'go bronxe':
		bronxe()
	elif x == 'go ny':
		ny()
	elif x == 'look':
		cleveland()
	elif x == 'exit':
		sys.exit(0)
	else:
		print 'Not Recognized.'
		clev_prompt()
	

def cop_fight():
	global hp, thugHP, cash
	charhp = hp
	thughp = thugHP
	print """YOU ARE IN A FIGHT!"""
	print """\
	
	   +++++++++++++++++
	   +      COP     +
	   +++++++++++++++++
	   
	   """
	while (hp > 0) and (thugHP > 0):
		
	# player attacks
		chardmg = random.randrange(2) + 1
		print 'You attack the COP!'
		print 'You deal ', chardmg, 'damage'
		   
		thugHP -= chardmg
		print "YOUR HP: ", hp
		print "COP HP: ", thugHP
		raw_input()
		   
		 # thug attacks
		thugdmg = random.randrange(1) + 1
		print 'The COP attacks you!'
		print 'The COP deals ', thugdmg, 'damage to you!'
		   
		charhp -= thugdmg
		print "Your HP: ", hp
		print 'COP HP: ', thugHP
		raw_input()
	if charhp < 0:
		print "The COP killed you."
		print "GAME OVER"
		gameover()
	else:
		print "You killed the COP!"
		cearn = random.randrange(10, 25)
		print "YOU EARNED ", cearn, "CASH!\n"
		cash = cash + cearn
		return 

def fight():
	global hp, thugHP, cash
	charhp = hp
	thughp = thugHP
	print """YOU ARE IN A FIGHT!"""
	print """\
	
	   +++++++++++++++++
	   +      THUG     +
	   +++++++++++++++++
	   
	   """
	while (hp > 0) and (thugHP > 0):
		
	# player attacks
		chardmg = random.randrange(2) + 1
		print 'You attack the thug!'
		print 'You deal ', chardmg, 'damage'
		   
		thugHP -= chardmg
		print "YOUR HP: ", hp
		print "THUG HP: ", thugHP
		raw_input()
		   
		 # thug attacks
		thugdmg = random.randrange(1) + 1
		print 'The thug attacks you!'
		print 'The thug deals ', thugdmg, 'damage to you!'
		   
		charhp -= thugdmg
		print "Your HP: ", hp
		print 'Thug HP: ', thugHP
		raw_input()
	if charhp < 0:
		print "The thug killed you."
		print "GAME OVER"
		gameover()
	else:
		print "You killed the thug!"
		cearn = random.randrange(10, 25)
		print "YOU EARNED ", cearn, "CASH!\n"
		cash = cash + cearn
		return 
		   

def cleveland():
	clear()
	global cash, dealers, hp, gang
	print "CASH: ", cash,  "dealers: ", dealers, "HP: ", hp, "GANG MEMBERS: ", gang
	print
	print 'you are in cleveland.'
	print 
	print 'Bob sells GUNS. Use "trade guns". Foo sells DEALERS. use "buy dealers".'
	print 'You should tell your dealers "dealers work".'
	print "You can also go : la, bronxe, NY"
	i = random.randrange(1, 5)
	if i == 1:
		print "2 thugs want to join your gang!"
		gang = gang + 2
		clev_prompt()
		
	elif i == 2:
		fight()
		clev_prompt()
	elif i == 3:
		print 'You find $50'
		cash = cash + 50
		clev_prompt() 
	elif i == 4:
		cop_fight()
		clev_prompt() 
	elif i == 5:
		print 'Two dealers want to join your crew.'
		dealers = dealers + 2
		clev_prompt()
	else:
		print "Error."
		clev_prompt()
	clev_prompt()
	
	
	


def game():
	cleveland()
	la()
	# bronxe()  TODO
	# ny()      TODO

def menu_prompt():
	x = raw_input("> ")
	if x == 'a':
		clear()
		game()
	elif x == 'b':
		clear()
		loadgame()
	elif x == 'c':
		clear()
		credits()
	else:
		print "THAT IS NOT A VALID OPTION."
		menu_prompt()

def clear():
	print """
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	"""


def menu():
	clear()
	print """\
	
	++ ++  ++  ++ +    +   ++ ++ ++++++ +    + +  +
	+ + + +  + ++ + +  +   + + + +      + +  + +  +
	+   + ++++ ++ +  + +   +   + +++    +  + + +  +
	+   + +  + ++ +   ++   +   + ++++++ +   ++ ++++
	
	
	A: PLAY NEW GAME
	B: LOAD GAME (COMING SOON)
	C: CREDITS (COMING SOON)
	"""
	menu_prompt()
	


def main():
	print """\
	++++ + ++ ++ ++++           +     +  +    +++++  +++++
	++++ + + + + ++++            + + +  +++   +++++  +
	+    + +   + +                + +  +   +  +  +   +++++
	+                                                    +
	+              BY: KARIMRUAN                     +++++     
	       
	"""
	raw_input()
	menu()
		  
main()

Any help would be greatly appreciated! I also attached the .py incase it is easier to test it without having to delete line numbers.

Recommended Answers

All 7 Replies

forgot the .py... but it won't let me add it. sorry...

What doesn't work? Does it give you an error? Or does it just not function properly? what does it do?

It just doesn't do what it is supposed to.

It is supposed to function like this:

1. user chooses to sell an item
2. game removes item from inventory
3. game adds resulting cash to inventory.

here is the offending code:

def foo_sell():
	global cash, dealers
	clear()
	print """\
	
	Sell how many DEALERS?
	
	a. 1 dealer for $50
	b. 2 dealers for $100
	c. 3 dealers for $150
	
	or:
	
	d. exit
	"""
	x = raw_input("> ")
	if x == 'a':
		if dealers > 1:
			print "You sell 1 dealer  for $50."
			dealers = dealers - 1
			cash = cash + 50
			foo_sell()
		else:
			print "You do not have enough dealers to sell."
			print 'You have ', dealers, ' dealers to sell.'
			foo_sell()
	elif x == 'b':
		if dealers > 2:
			print 'You sell 2 dealers for $100.'
			dealers = dealers - 2
			cash = cash + 100
			foo_sell()
		else:
			print "You do not have enough to sell 2 dealers."
			print "You have ", dealers, " dealers to sell."
			foo_sell()
	elif x == 'c':
		if dealers > 3:
			print 'You sell 3 dealers for $150.'
			dealers = dealers - 3
			cash = cash + 150
			foo_sell()
		else:
			print 'You do not have enough to sell 3 dealers.'
			print 'You have ', dealers, ' dealers to sell.'
			bob_sell()
	elif x == 'd':
		cleveland()

Sure you gone have big problems.
Global variables is a really bad thing,and you have used it a lot.

def la():
    clear()
    global cash, dealers, hp, gang

def foo_sell():
    global cash, dealers

Now cash,dealer is in global namespace.

global cash  #this is global namespace so no need to use global here.
cash = 500
global dealers
dealers = 2

Now do understand why this is a big mess?
This happen many times in your code an make global namespace a big mess off variables.
An important point with function is to keep data local to that function.
Not all variables get thrown out in global namespace.

I am not sure if someone can help you,without rewrite a lot off code.
You shold try to make normal function that takes argument and return data and remove all global.

I have never had a problem with declaring globals like this before. I have tried and tried to come up with a fix, but, to no avail. which is why I came here, as a last resort. Does anybody have an idea of how to fix this?

This program should use a class. For the foo_sell() function as it is, pass 'cash' and 'dealers' to it and return them as well, avoiding globals.

I have never had a problem with declaring globals like this

You have now and hopefully have learned that lesson.

Well, I took everyone's advice and used classes. It does work a lot better. I even added a gang war to the demo, but, your gang wins with one turn. I am including the code for the gangfight() function and all necessary classes so if anyone sees the problem, let me know so I can fix it.

# player class

class player():
	hp = 25
	cash = 500
	dealers = 2
	gang = 5
	guns = 0
	ganghp = 20
class hawksgang():
	ganghp = 25
	members = random.randrange(4, 10)
def gangfight():
	charhp = player.ganghp * player.gang
	thughp = hawksgang.ganghp * hawksgang.members
	print "The Hawks gang is attacking you!"
	print "Your gang: ", player.gang
	print "Hawks gang: ", hawksgang.members
	
	# player gang attacks
	
	chardmg = random.randrange(2) + 1
	attack = chardmg * player.gang
	print "Your gang attacks the Hawks!"
	print "You deal ", attack, "damage!"
	
	thughp -= attack
	print 
	print "Your gangs HP: ", charhp
	print "Hawks HP: ", thughp
	
	# hawks attack
	
	thugdmg = random.randrange(1) + 1
	attack = thugdmg * hawksgang.members
	print "The Hawks attack you!"
	print "They deal ", attack, "damage to your gang!"
	
	charhp -= attack
	print "Your gangs HP: ", charhp
	print "Hawks HP: ", thughp
	
	raw_input()
	if charhp < 1:
		print "The hawks have defeated your gang."
		print "Game Over."
		gameover()
	else:
		print "Your gang has defeated the Hawks!"
		cearn = random.randrange(10, 25)
		income = cearn * hawksgang.members
		print "You earned $", income ,"!\n"
		player.cash += income
		return

Now, I am a beginner, so let's try to keep to constructive criticism, not flaming for any errors or condescending for lack of organisation and the likes. I know it is not the best written code, but with some of the rewrites I will most likely be doing once I get it how I want it, it should be better. I learn by doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.