Python游戏不起作用(商店只出现一次)

Python游戏不起作用(商店只出现一次),python,debugging,python-2.7,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 t

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

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: ")
money=100
娱乐=30
剩余=15
社会福利=30
inv=1
食物=30
分数=1
def命令():
打印“命令:”
打印“'P'打印命令选项”
打印“'S'去商店”
打印“工作列表?”
打印“'M'市场”
打印“'F'农场”
打印“'H'呆在家里休息”
打印“'E'娱乐/去展会”
def shop():
打印“店铺:”
打印“这是商店,你用赚来的钱买东西”
打印“按‘I’键买土豆!这会给你额外20个库存点!花费80美元!”
打印“按‘R’键以获得更好的床!这将为您提供20个额外的休息点!花费80美元!”
打印“Press’S”作为短信计划!这会给你额外的20点社交积分!花费80美元
打印“按‘E’键以获得更好的电视!这将为您提供20个额外的娱乐点数!花费80美元!”
打印“再次按下此列表的“H”!”
打印“按“L”返回游戏!”
随机输入
导入系统
命令()
def do_farm():
娱乐=娱乐-随机。随机范围(1,7+1)
rest=rest-random.randrange(1,7+1)
社交=社交-随机。随机范围(1,10+1)
食物=食物+随机。随机范围(1,7+1)
库存=库存+随机。随机范围(1,30+1)
money=money-random.randrange(1,10+1)
分数=分数+1
打印“金钱=%d,娱乐=%d,休息=%d,社交=%d,库存%d,食品%d,分数%d”%(金钱,娱乐,休息,社交,库存,食品,分数)

如果钱你从来没有清理过
shop\u-choice
,那么下一次有人试图去商店时,他们会立即离开(因为
shop\u-choice
在他们上次访问然后离开商店时已经设置为
L

除了Amber发现的问题,你有一些缩进问题,这会使你很难到达商店


shop==“p”
检查在
rest中,这是一个相当大的控制流。您是否能够隔离导致问题的代码部分?不是您试图解决的问题,而是…第一次,您将
choice
shop\u choice
设置为小写
“p”
,这与
choice==“p”
不匹配。(同样,如果用户键入一个小写的
p
它下次将不匹配。)您可能希望所有这些检查都是
choice.upper()=“p”
之类的,或者更好的是,用
choice=raw\u input(…)
替换
choice=raw\u input(…)。upper()
。顺便说一句:构建大型交换机(elif语句)像您这样做被认为是一种糟糕的编程实践。至少尝试合并您的条件(您检查不同的条件,但它们都导致相同的打印和退出)在这里,你可以阅读更多:那么我该如何清除它?@ImGone98:问题是你在调用
raw\u input
之前正在检查值,因此它具有你恰好留在那里的任何值。更好的方法是将
raw\u input
移动到循环的顶部。没有代码,这很难解释,所以我把它放在我的位置我要做的是,一开始我没有考虑它,谢谢(:@ImGone98:如果你是指最后一部分……我猜你不想在True
break
时做
,而想“一定有更好的方法”…但不幸的是,如果不严格重组代码,没有更好的方法了。我添加了所有这些,但现在它给了我错误?@ImGone98:问题是你试图使用全局变量,但没有声明它们
global
。有时Python可以猜测它们是全局的,但如果不能,你会得到一个
UnboundLocalE错误
。您可以通过将
全局娱乐
添加到函数体顶部来修复此问题。更好的修复方法是重新组织代码,使其不需要全局变量。例如,如果您创建了一个类,所有这些值都作为实例变量,所有这些函数都作为方法,则它们可以执行
自娱乐
,等等。但是在评论中要解释的太多了。
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
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