python无限循环写法_Python文本冒险游戏输入无限循环

本文介绍了一个初学者创建的Python文本冒险游戏,玩家在木屋前廊场景中与物体互动。遇到的问题是输入特定命令如“查看库存”和“查看环境”时,程序只会重复请求输入,而不会执行相应操作。代码包括了物品交互、重量管理、场景描述等功能,但存在逻辑问题导致无限循环无法正确响应。
摘要由CSDN通过智能技术生成

我是一个初学者,通过创建一个文本冒险游戏来学习编写python代码。我使用多个资源,没有教程。我已经创建了一个开始的场景,它会要求您输入或执行命令。我的命令与物体(扫帚和椅子)相互作用,效果很好。我在更换场景、查看库存和查看周围环境方面遇到了问题。当我输入这些命令时,它只是再次请求我的输入,而不是显示清单、描述我的周围环境(使用cabinfrontporchation函数)并将我的位置更改为悬崖并显示有关悬崖的信息。

这是我的全部代码,我在用#分割时遇到了麻烦

# text game (similar to zork)

name = 1

location = "cabinFrontPorch"

loadWeight = 0

description = {

"chair" : "It is a wooden chair there is really nothing special about it. Probably good for pondering lifes greatest questions.",

"broom": "It is a wooden broom with old-fasioned straw bristles. It looks like a witch would love one of these bad boys!"

}

weightList ={

"broom": 3,

}

inventory = [

"",

]

cabinFrontPorch = [

"chair",

"broom",

]

heavy = [

"chair",

]

goCommand = [

"go north",

"go south",

"go east",

"go west",

"go up",

"go down",

"go northwest",

"go northeast",

"go southeast",

"go southwest",

]

objectCommand = [

"get",

"drop",

"push",

"pull",

"examine",

"use",

"attack"

]

def addWeight(x):

global loadWeight;

loadWeight = loadWeight + x;

def listItem(list):

for item in list:

print("There is also a " + item)

def objectAction(object, location, action):

if object in inventory or object in location:

if action == "get":

if object not in inventory:

if loadWeight <= 10:

if object in heavy:

#Create heavy list

print("The " + object + "is too heavy to carry")

else:

inventory.append(object)

location.remove(object)

print("Taken.")

x = weightList[object]

addWeight(x)

print("Your are carrying " + str(loadWeight) + "/10 units of weight.")

else:

print("You have too much stuff in your hands")

else:

print("You already have that in your hand")

elif action == "push":

if object not in inventory:

if object in pushable:

print("This object is pushable")

else:

print ("Why are you so intent on disturbing the " + object + "? It is just a " + object + "!")

else:

print("You can't push this because it is in your inventory")

elif action == "pull":

if object not in inventory:

if object in pullable:

print("This object is pullable")

else:

print ("Why are you so intent on disturbing the " + object + "? It is just a " + object + "!")

else:

print("You can't pull this because it is in your inventory")

elif action == "examine":

print(description[object])

#write description dictionary

elif action == "use":

print("You sit down for a while taking this time to enjoy life and ponder the deeper philosophies if the world. You realize that there is an entire world out there that is yours for the taking but you also know that you aren't going to accomplish anything just sitting in a chair all day. It is with this that you realize you just wasted time by sitting in a chair.")

elif action == "drop":

if object in inventory:

print("Dropped.")

location.append(object)

inventory.remove(object)

else:

print("You can't drop something that's not already in your inventory")

elif action == "attack":

print("I've seen some crazy stuff but I've never seen someone attack an " + object + "!")

else:

print("The " + object + " is not in this area.")

def intro():

print("Welcome to Korz!")

print("")

print("In this world of interactive fiction you will")

print("find lots of treasure, monsters and puzzles to")

print("test the limits of your human mind")

print("You will be able to LOOK at your surroundings")

print("EXAMINE certain objects")

print("USE objects")

print("ATTACK monsters")

print("OPEN and CLOSE doors")

print("PUSH and PULL objects")

print("GET and DROP items")

print("You may view your inventory at anytime")

print("And perhaps most importantly GO places")

print("(UP DOWN NORTH SOUTH EAST WEST)")

print("It is now up to you to use this virtual world")

print("To explore all of the possibilities and to have")

print("an amazing ADVENTURE")

name = input("What is your NAME Adventurer? \n>>>")

print ("Allright " + name +"! You are on your own from here! Have Fun!")

cabinFrontPorchAction()

def cabinFrontPorchAction():

location = "cabinFrontPorch";

print("You are standing on the porch on the north side of a log cabin")

print("To the north of the porch is a forest with lush evergreen trees")

print("East of the cabin is a creek that runs north to south through the woods")

print("West of the cabin are sheer cliffs that look out over a vast blue ocean")

listItem(cabinFrontPorch)

while location == "cabinFrontPorch":

requestInputCFP()

###########################################

def requestInputCFP():

action = input(">");

action = action.lower();

action = action.split();

if action in goCommand:

if action == "go south":

print ("The door is locked shut. To go this direction unlock the door")

elif action == "go west":

cliff()

elif action == "inventory":

if not inventory:

print("You are empty handed")

else:

print(inventory)

elif action == "look":

cabinFrontPorchAction();

elif len(action) > 1:

if action[1] == "broom":

objectAction("broom" ,cabinFrontPorch, action[0])

elif action[1] == "chair" or action[1] == "wooden":

objectAction("chair" , cabinFrontPorch, action[0])

def cliff():

location = "cliff";

print("You come up to the edge of a sheer cliff just east of a vast blue ocean.")

print("It would be impossible to get down to the beach this way.")

print()

########################################################

intro()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值