-1

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

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 PS如果你想提出一个可以添加的想法,你可以这样做!编辑:更改代码,新错误

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
4

2 回答 2

3

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

于 2013-05-01T22:01:48.363 回答
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 True您必须执行 a和 a而不是将条件直接放入循环中,这有点烦人break,但另一种方法是编写相同的输入代码两次(一次在循环之前,一次在结束时)而不是一次(在循环的顶部),或者编写可以处理“还没有输入”状态的复杂代码。

于 2013-05-01T22:20:38.357 回答