Python game not working(shop only showing up once)

I'm trying to make just a simple text game before I go on learning more things, the first part went good, then I tried to add a shop and when the code is run the shop can only be entered once. Here is the code

money = 100
entertainment = 30
rest = 15
social = 30
inv = 1
food = 30
score = 1

def commands():
    print "Commands:"
    print " 'P' Print commands options"
    print " 'S' Go to the shop"
    print "Job list? "
    print " 'M' Market"
    print " 'F' Farm"
    print " 'H' Stay at home a rest"
    print " 'E' Entertainment/go to fair"
def shop():
    print "Shop:"
    print " This is the shop, you buy things with your money that you gain"
    print "Press 'I' for a potato! This gives you 20 extra Inventory points! Costs 80 money!"
    print "Press 'R' for a better bed! This gives you 20 extra rest points! Costs 80 money!"
    print "Press 'S' for a texting plan! This gives you 20 extra social points! Costs 80 money!"
    print "Press 'E' for a better tv! This gives you 20 extra entertainment points! Costs 80 money!"
    print "Press 'H' for this list again!"
    print "Press 'L' to return to your game!"
import random
import sys
commands()
def do_farm():
    entertainment = entertainment - random.randrange(1,7+1)
    rest = rest - random.randrange(1,7+1)
    social = social - random.randrange(1,10+1)
    food = food + random.randrange(1,7+1)
    inv = inv + random.randrange(1,30+1)
    money = money - random.randrange(1,10+1)
    score = score + 1
    print "Money = %d, Entertainment = %d, Rest = %d, Social = %d, Inventory %d, Food %d, Score %d" % (money, entertainment, rest, social, inv, food, score)
    if money <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif food <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif social <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif entertainment <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif rest <= 0:
            print "Game over, Score: %d" % (score)
            exit()
def do_home():
    entertainment = entertainment + random.randrange(1,3+1)
    rest = rest + random.randrange(1,7+1)
    social = social + random.randrange(1,5+1)
    food = food - 5
    money = money - random.randrange(1,10+1)
    score = score + 1
    print "Money = %d, Entertainment = %d, Rest = %d, Social = %d, Inventory %d, Food %d, Score %d" % (money, entertainment, rest, social, inv, food, score)
    if money <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif food <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif social <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif entertainment <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif rest <= 0:
            print "Game over, Score: %d" % (score)
            exit()
def do_ent():
    entertainment = entertainment + random.randrange(1,7+1)
    social = social + random.randrange(1,5+1)
    food = food - 3
    money = money - random.randrange(1,10+1)
    score = score + 1
    print "Money = %d, Entertainment = %d, Rest = %d, Social = %d, Inventory %d, Food %d, Score %d" % (money, entertainment, rest, social, inv, food, score)
    if money <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif food <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif social <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif entertainment <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif rest <= 0:
            print "Game over, Score: %d" % (score)
            exit()
def do_market():
    entertainment = entertainment - random.randrange(1,7+1)
    rest = rest - random.randrange(1,7+1)
    social = social + random.randrange(1,5+1)
    food = food - 5
    money = (inv * 1.5) + money
    inv = 1
    score = score + 1
    print "Money = %d, Entertainment = %d, Rest = %d, Social = %d, Inventory %d, Food %d, Score %d" % (money, entertainment, rest, social, inv, food, score)
    if money <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif food <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif social <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif entertainment <= 0:
            print "Game over, Score: %d" % (score)
            exit()
    elif rest <= 0:
            print "Game over, Score: %d" % (score)
            exit()
def do_shop_inventory():
    score = score + 10
    inv = inv + 20
    money = money - 80
def do_shop_rest():
    score = score + 10
    rest = rest + 20
    money = money - 80
def do_shop_social():
    score = score + 10
    social = social + 20
    money = money - 80
def do_shop_ent():
    score = score + 10
    entertainment = entertainment + 20
    money = money - 80
def shop_commands():
    while True:
        shop()
        shop_choice = raw_input("Shop command: ")
        if shop_choice == "I":
            do_shop_inventory()
        elif shop_choice == "R":
            do_shop_rest()
        elif shop_choice == "S":
            do_shop_social()
        elif shop_choice == "E":
            do_shop_ent()
        elif shop_choice == "H":
            shop()
        elif shop_choice == "L":
            break
choice = raw_input("Your command: ")
while choice != "Q":
    if choice == "F":
        do_farm()
    elif choice == "H":
        do_home()
    elif choice == "E":
        do_ent()
    elif choice == "M":
        do_market()
    elif choice == "S":
        shop_commands()
    commands()
    choice = raw_input("Your command: ")

I am fairly new with Python, about 2-4 weeks. So please no complex answers if possible:D

I would like to know what is wrong and an idea of how to fix it. Thanks:D P.S. if you want to suggest an idea that could be added you could do that to! EDIT: Changed code, new error

Traceback (most recent call last):
  File "C:\Users\ImGone\Desktop\MoneySurvival_bakcup.py", line 173, in <module>
    do_farm()
  File "C:\Users\ImGone\Desktop\MoneySurvival_bakcup.py", line 46, in do_farm
    entertainment = entertainment - random.randrange(1,7+1)
UnboundLocalError: local variable 'entertainment' referenced before assignment

Answers


In addition to the problem Amber found, you've got some indentation problems that will make it hard to get to the store.

The shop == "P" check is inside the rest <= 0 case inside the choice == "F" case. So, the only way to get to the shop is to go to the farm and use up all your rest. That can't be right.

This would be a lot simpler if you factored out your code into functions, like this:

def do_farm():
    entertainment = entertainment - random.randrange(1,7+1)
    # ...

And then your main loop could just do this:

if choice == "F":
    do_farm()
elif choice == "S":
    do_shop()
# ...

Also, it's a lot easier to get the loop right if you do things the other way around, asking for the choice at the top of the loop instead of the bottom. For example:

def shop():
    while True:
        shop()
        shop_choice = raw_input("Shop command: ")
        if shop_choice == "I":
            do_shop_inventory()
        elif shop_choice == "R":
            # ...
        elif shop_choice == "L":
            break

It is a bit annoying that you have to do a while True and a break instead of putting the condition directly in the loop, but the alternative is to write the same input code twice (once before the loop, and once again at the end) instead of once (at the top of the loop), or write convoluted code that can handle a "no input yet" state.


You never clear out shop_choice, so the next time someone tries to go to the shop, they instantly leave it (because shop_choice is already set to L from the previous time they visited and then left the shop).


Need Your Help

How to copy the IPTC data from one image to another?

php metadata iptc

I'm developing an image processor script to basically resize images. The problem is that when I make the resized copy of the image, it doesn't copy the IPTC data as well. So I've been checking the ...

CNAME for Omntiure

performance browser cookies cname adobe-analytics

What are some reasons why sites like LLNW create a CNAME record for Omniture requests (e.g. metrics.limelightnetworks.com instead of limelightnetworks.122.2o7.net)?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.