Python 3中基于文本的冒险

Python 3中基于文本的冒险,python,python-3.x,Python,Python 3.x,我正在开发基于文本的RPG,但我发现了许多问题,如: 进店后无处可去 由于某种原因,我无法进入“酒馆”;及 电脑只是说“购买不是十字弓定义的” 代码: gold=int(100) 库存=[“剑”、“盔甲”、“药剂”] 打印(“欢迎英雄”) name=输入(“您的姓名:”) 打印(“你好”,姓名,) #角色扮演程序 # #在力量、健康、智慧和灵巧上花费30点 #玩家可以从任何属性花费和获取积分 经典={“勇士”, “弓箭手”, “法师”, “治疗者”} 打印(“从中选择您的种族”,经典,) c

我正在开发基于文本的RPG,但我发现了许多问题,如:

  • 进店后无处可去
  • 由于某种原因,我无法进入“酒馆”;及
  • 电脑只是说“购买不是十字弓定义的”
代码:

gold=int(100)
库存=[“剑”、“盔甲”、“药剂”]
打印(“欢迎英雄”)
name=输入(“您的姓名:”)
打印(“你好”,姓名,)
#角色扮演程序
#
#在力量、健康、智慧和灵巧上花费30点
#玩家可以从任何属性花费和获取积分
经典={“勇士”,
“弓箭手”,
“法师”,
“治疗者”}
打印(“从中选择您的种族”,经典,)
classicChoice=input(“您选择什么类:”)
打印(“您现在是”,classicChoice,)
#库包含属性和点
属性={“强度”:int(“0”),
“运行状况”:“0”,
“智慧”:“0”,
“灵巧度”:“0”}
池=int(30)
选择=无
印刷品(“塑造英雄!!!”)
打印(属性)
打印(“\n您有”,池,“要花费的点数”)
而选择!="0":
#选择列表
印刷品(
"""
选项:
0-结束
1-向属性添加点
2-从属性中删除点
3-显示属性
"""
)
选择=输入(“选择选项:”)
如果选项==“0”:
打印(“\n您的英雄属性为:”)
打印(属性)
elif选项==“1”:
打印(“\nADD指向属性”)
打印(“您有”,池,“要花费的点数”)
印刷品(
"""
选择一个属性:
力量
健康
智慧
灵巧
"""
)
at_choice=输入(“您的选择:”)
如果属性中的at_choice.lower():
points=int(输入(“要分配多少个点:”)

如果您第一次在第111行询问用户去哪里,如果他们输入了除“shop”之外的内容,会发生什么情况?然后第119行的
if choice==“shop”:
条件将失败,并且
buy=input(“…”)
将永远不会执行。此时,
buy
不存在,因此当下一个条件执行时,它崩溃,因为如果buy==“crossbow”
,它无法计算
buy
没有价值,因此您无法将其与任何东西进行比较

您需要缩进所有的商店逻辑,以便它位于
if choice==“shop”
块中

if choice == "shop":
    print("Welcome to the shop!")
    print("You have", gold,"gold")
    buy = input("What would you like to buy? A crossbow, a spell or a potion: ")

    if buy == "crossbow":
        print("this costs 50 gold")
        #...etc
    if buy == "spell":
        print("this costs 35 gold")
同样的问题也存在于你的酒馆代码中。即使你不去酒馆,你也要检查
酒馆选择
。这也需要缩进

if choice == "teavern":
    print("You enter the tavern and see a couple of drunken warriors singing, a landlord behind the bar and a dodgy figure sitting at the back of the tavern.")
    tavernChoice = input("Would you like to talk to the 'drunken warriors', to the 'inn keeper', approach the 'dodgy figure' or 'exit'")

    if tavernChoice == "drunken warriors":
        print("You approach the warriors to greet them.")
在这一点上,您的程序将结束,但我猜您希望继续能够探索这些领域。您可以从第一个
input
命令开始,将所有内容放入
while
循环中

while True:
    print("Here is your inventory: ", inventory)
    print("What do you wish to do?")
    print("please input shop, tavern, forest.")
    choice = input("Go to the shop, go to the tavern, go to the forest: ")

    if choice == "shop":
        print("Welcome to the shop!")
        #...etc

    elif choice == "tavern":
        print("You enter the tavern...")
        #...etc

    elif choice == "forest":
        print("You enter the forest...")
        #etc

    else:
        print("Not acepted")
        print("What do you wish to do?")
        print("please input shop, tavern, forest.")

这也比您当前的方法更简洁,因为您只需询问用户一次他们要去哪里,而不是在第112、165和170行上问三次。

基于Kevin的回答,这里有一个压缩冗余的版本,因此您只需添加一个新的
def go_xyz():…
靠近顶部

def go_shop():
    print("Welcome to the shop!")
    #...etc

def go_tavern():
    print("You enter the tavern...")
    #...etc

def go_forest():
    print("You enter the forest...")
    #etc

places = {name[3:]:globals()[name] for name in globals() if name.startswith('go_')}
placenames = ', '.join(places)

inventory = ['book']
retry = False

while True:
    if not retry:
        print("Here is your inventory: ", inventory)
    else:
        print("I do not know that place")
    print("Where do you wish to go?")
    print("Possible places: ", placenames, '.')
    choice = input("? ")
    try:
        places[choice]()
        retry = False
    except KeyError:
        retry = True
def go_shop():
    print("Welcome to the shop!")
    #...etc

def go_tavern():
    print("You enter the tavern...")
    #...etc

def go_forest():
    print("You enter the forest...")
    #etc

places = {name[3:]:globals()[name] for name in globals() if name.startswith('go_')}
placenames = ', '.join(places)

inventory = ['book']
retry = False

while True:
    if not retry:
        print("Here is your inventory: ", inventory)
    else:
        print("I do not know that place")
    print("Where do you wish to go?")
    print("Possible places: ", placenames, '.')
    choice = input("? ")
    try:
        places[choice]()
        retry = False
    except KeyError:
        retry = True