Python游戏无法正常工作(商店只出现一次)

时间:2013-05-01 21:58:26

标签: python debugging python-2.7

我正在尝试制作一个简单的文字游戏,然后我继续学习更多的东西,第一部分很好,然后我尝试添加一个商店,当代码运行时,商店只能输入一次。这是代码

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: ")

我对Python很新,大概2-4周。所以,如果可能的话,请不要复杂的答案:D

我想知道什么是错的,并想知道如何解决它。 感谢:D 附:如果你想提出一个可以添加的想法,你可以这样做! 编辑: 更改了代码,新错误

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

2 个答案:

答案 0 :(得分:3)

您永远不会清除shop_choice,所以下次有人试图去商店时,他们会立即离开(因为shop_choice已经设置为L访问然后离开商店)。

答案 1 :(得分:0)

除了Amber发现的问题之外,你还有一些缩进问题会让你难以到达商店。

shop == "P"检查位于rest <= 0案例中的choice == "F"案例内。所以,去商店的唯一方法就是到农场去休息。这不可能是正确的。

如果将代码分解为函数,这会更简单:

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

然后你的主循环可以这样做:

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

另外,如果你以相反的方式做事情,那么让循环更容易,要求在循环顶部而不是底部进行选择。例如:

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

您必须执行while Truebreak而不是将条件直接放在循环中,这有点令人讨厌,但另一种方法是将相同的输入代码写入两次(之前一次)循环,再次在结束时)而不是一次(在循环的顶部),或者编写可以处理“无输入”状态的复杂代码。