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