Python 3.x在使用';def&x27;方法
然而,我的代码是一个基于文本的冒险程序,当我运行它时,它尝试使用它中断的变量,因为变量没有被添加到def中,因为python在做其他事情之前定义了一些事情 这是我的代码,当你往北走两次,打开窗口,输入window和up或east时,程序就会中断Python 3.x在使用';def&x27;方法,python,python-3.x,function,Python,Python 3.x,Function,然而,我的代码是一个基于文本的冒险程序,当我运行它时,它尝试使用它中断的变量,因为变量没有被添加到def中,因为python在做其他事情之前定义了一些事情 这是我的代码,当你往北走两次,打开窗口,输入window和up或east时,程序就会中断 import time reset = "yes" def print_slow(str): for letter in str: print(letter), time.sleep(.3) def ga
import time
reset = "yes"
def print_slow(str):
for letter in str:
print(letter),
time.sleep(.3)
def game():
#This sets the commands to be used
inv_command = ["inv", "inventory", "i"]
west = ["west", "w", "go west", "walk west"]
east = ["east", "e", "go east", "walk east"]
north = ["north", "n", "go north", "walk north"]
south = ["south", "s", "go south", "walk south"]
northeast = ["northeast", "ne", "go northeast", "walk northeast"]
northwest = ["northwest", "nw", "go northwest", "walk northwest"]
southeast = ["southeast", "se", "go southeast", "walk southeast"]
southwest = ["southwest", "sw", "go southwest", "walk southwest"]
up = ["up", "u", "go up", "go up stairs", "go up ladder", "up stairs", "up ladder"]
down = ["down", "d", "go down", "go down stairs", "go down ladder", "down stairs", "down ladder"]
take = ["take", "t"]
die = ["die","suicide", "kill myself","kill"]
#This sets the objects
mailbox = 1
house_window = 0
house_bag = 0
knife = 0
lamp = 0
carpet = 0
rug = 0
light = 0
trapdoor =0
#This sets the inventory
class Item(object):
def __init__(self, name, attack, armour):
self.name = name
self.attack = attack
self.armour = armour
class Inventory(object):
def __init__(self):
self.items = {}
def add_item(self, item):
self.items[item.name] = item
def contain_item(self, item):
self.items["Leaflet"] = item
def print_items(self):
print('\t'.join(['Name', 'Atk', 'Arm']))
for item in self.items.values():
print('\t'.join([str(x) for x in [item.name, item.attack, item.armour]]))
time.sleep(1)
inventory = Inventory()
class death():
def kill():
print("You killed yourself")
reset()
game = 1
still_running = 1
while game is still_running:
#This is the spawn area
def spawn():
while game is still_running:
inp = input("You wake up in a field surrounded by trees. The only pathway is going north \n>")
if inp in north:
front_house()
if inp in die:
game.reset()
elif inp in inv_command:
inventory.print_items()
else:
print("<You can't do that>")
#This is the front of the house
def front_house():
while game is still_running:
inp = input("You face the front of a house. The windows are boarded up and the door is closed. In front there is a mailbox, and pathways leading west and further north\n>")
if inp in south:
spawn()
elif inp in inv_command:
inventory.print_items()
elif inp in north:
back_house()
elif inp in west:
west_forest_entrance()
elif inp in "open door":
print("<The door is locked>")
elif inp in "open window":
print("<The windows can't be opened. They seem to be nailed shut>")
elif inp in "read leaflet":
if inventory.contain_item:
print("<Welcome to the text based adventure!>")
print("<When typing commands, please don't use capital letters.>")
print("<have fun!>")
else:
print("<You don't have a leaflet>")
elif inp in "open mailbox":
if mailbox == 1:
print("<The mailbox contains a leaflet>")
else:
print("<The mailbox is empty>")
elif inp in "take leaflet":
if mailbox == 1:
inventory.add_item(Item('Leaflet', 0, 0))
mailbox = 0
print("<You have taken the leaflet>")
else:
print("<There's no leaflet in the mailbox>")
else:
print("<You can't do that>")
#This is the back of the house
def back_house():
while game is still_running:
inp = input("You face the back of the house, which has nothing except one ajar window. There is another pathway heading east \n>")
if inp in inv_command:
inventory.print_items()
if inp in south:
front_house()
if inp == "open window":
house_window = 1
print("<You opened the window enough for you to enter through>")
if inp == "enter window":
if house_window == 1:
kitchen()
else:
print("<The window isn't opened enough to fit through>")
else:
print("<You can't do that>")
#This is the kitchen
def kitchen():
while game is still_running:
inp = input("You are inside the kitchen of the house. On the table there is a bag, there is a staircase leading up stairs and to the east there is a living room \n>")
if inp in inv_command:
inventory.print_items()
elif inp in "read manual":
if inventory.contain_item:
print("<Commands - inventory, take, open, attack>")
print("<Moving commands - go north, go south, go northeast, go northwest, go southeast, go southwest, up, down.>")
print("<Good luck!>")
elif inp in up:
attic()
elif inp in east:
living_room()
elif inp in "exit window":
back_house()
elif inp == "open bag":
print("<The bag contains a manual>")
elif inp == "take bag":
print("<You would much rather take what's inside the bag then the bag itself>")
elif inp == "take manual":
if house_bag == 0:
print("<You took the manual>")
house_bag = 1
inventory.add_item(Item('manual', 0, 0))
else:
print("<You have already taken the manual>")
else:
print("<You can't do that>")
#This is the Attic of the house
def attic():
while game is still_running:
if knife == 0:
if lamp == 0:
inp = input("You are in the attic, you can see a lamp and a knife. You see blood stains and stairs downwards \n>")
else:
inp = input("You are in the attic, you can see a knife. You see blood stains and stairs downwards \n>")
elif knife == 1:
if lamp == 0:
inp = input("You are in the attic, you can see a lamp. You see blood stains and stairs downwards \n>")
else:
inp = input("You are in the attic. You see blood stains and stairs downwards \n>")
if inp in inv_command:
inventory.print_items()
elif inp in down:
kitchen()
elif inp in "take knife":
if knife == 0:
print("You pulled the knife out from the wall and put it in your pocket")
print("<You have obtain a knife>")
knife = 1
inventory.add_item(Item('knife', 3, 0))
else:
print("<You have already taken the knife>")
elif inp in "take lamp":
if lamp == 0:
print("<You have taken the lamp>")
lamp = 1
inventory.add_item(Item('lamp', 0,0))
else:
print("<You have already taken the lamp>")
else:
print("<You can't do that>")
#This is the living room
def living_room():
while game is still_running:
if rug == 0:
inp = input("You are in the living room and you see a rug on the floor.\n>" )
if rug == 1:
inp = input("You are in the living room and you can see a trap door.\n>")
if inp == "move rug":
rug = 1
print("<You have found a trap door to the basement>")
if trapdoor == 1:
if inp in down:
underground_entrance()
if inp in inv_command:
inventory.print_items()
else:
print("<You can't do that>")
#This is the underground entrance
def underground_entrance():
while game is still_running:
if lamp == 1:
if light == 0:
inp = input("It is dark, you can't see anything.\n>")
if inp in up:
living_room()
if inp in inv_command:
inventory.print_items()
if inp == "turn on lamp" or "turn lamp on":
print("<You turned on the lamp>")
light = 1
else:
print("<You can't do that>")
if light == 1:
inp = input("You are below the living room, where you can see a entrance to a underground tunnel north of you.\n>")
if inp in up:
living_room()
if inp in inv_command:
inventory.print_items()
if inp in north:
loop = 12
else:
print("<You can't do that>")
if lamp == 0:
inp = input("It is dark, you can't see anything you need to find a lamp.\n>")
if inp in up:
living_room()
if inp in inv_command:
inventory.print_items()
else:
print("<You can't do that>")
#This is the west forest entrance
def west_forest_entrance():
while game is still_running:
inp = input("You are now in a dense forest. There is a fork in the road going NorthWest and SouthWest \n>")
if inp in inv_command:
inventory.print_items()
elif inp in east:
loop = 2
elif inp in northwest:
loop = 6
elif inp in southwest:
loop = 7
else:
print("<You can't do that>")
spawn()
def reset():
if reset == "yes":
print_slow("Resetting...")
print_slow("Loading...")
time.sleep(5)
game()
game()
导入时间
重置=“是”
def打印速度慢(str):
对于str中的字母:
印刷(信件),
时间。睡眠(.3)
def game():
#这将设置要使用的命令
库存命令=[“库存”、“库存”、“i”]
西部=[“西部”、“西部”、“向西走”、“向西走”]
东=[“东”、“东”、“向东”、“向东走”]
北=[“北”、“北”、“向北”、“向北走”]
南部=[“南部”、“南部”、“向南走”、“向南走”]
东北=[“东北”、“东北”、“向东北走”、“向东北走”]
西北=[“西北”、“西北”、“向西北走”、“向西北走”]
东南=[“东南”、“东南”、“向东南走”、“向东南走”]
西南=[“西南”、“西南”、“向西南走”、“向西南走”]
up=[“up”,“u”,“go up”,“go up stairs”,“go up up ladder”,“up stairs”,“up up ladder”]
down=[“down”、“d”、“down”、“down”、“down楼梯”、“down梯子”、“down楼梯”、“down梯子”]
take=[“take”,“t”]
死亡=[“死亡”,“自杀”,“自杀”,“自杀”]
#这将设置对象
邮箱=1
房子窗户=0
house_bag=0
刀=0
灯=0
地毯=0
地毯=0
灯光=0
活板门=0
#这将设置库存
类别项目(对象):
定义初始(自我、姓名、攻击、装甲):
self.name=名称
自我攻击
盔甲
类别清单(对象):
定义初始化(自):
self.items={}
def添加_项(自身,项):
self.items[item.name]=项目
def包含_项(自身,项):
自我物品[“传单”]=物品
def打印_项(自身):
打印('\t'.join(['Name','Atk','Arm']))
对于self.items.values()中的项:
打印('\t'.join([item.name,item.attack,item.armor]]中的[str(x)代表x)])
时间。睡眠(1)
存货=存货()
类别死亡():
def kill():
打印(“你自杀了”)
重置()
游戏=1
仍在运行=1
当游戏仍在运行时:
#这是产卵区
def spawn():
当游戏仍在运行时:
inp=input(“你在树木环绕的田野中醒来。唯一的路径是向北走\n>”)
如果inp位于北部:
前(屋)
如果inp在模具中:
游戏重置()
inv_命令中的elif inp:
库存。打印_项()
其他:
打印(“”)
“阅读传单”中的elif inp:
如果inventory.contain_项目:
打印(“”)
打印(“”)
“打开邮箱”中的elif inp:
如果邮箱==1:
打印(“”)
其他:
打印(“”)
“索取传单”中的elif inp:
如果邮箱==1:
存货。添加项目(项目(‘传单’,0,0))
邮箱=0
打印(“”)
其他:
打印(“”)
#这是房子的后面
def back_house():
当游戏仍在运行时:
inp=input(“你面对的是房子的后面,除了一扇半开的窗户外什么都没有。还有一条小路向东\n>”)
如果inp在inv_命令中:
库存。打印_项()
如果inp位于南部:
前(屋)
如果inp==“打开窗口”:
房子窗户=1
打印(“”)
如果inp==“进入窗口”:
如果house_window==1:
厨房()
其他:
打印(“”)
#这是厨房
def kitchen():
当游戏仍在运行时:
inp=input(“你在房子的厨房里。桌子上有一个包,有一个楼梯通向楼梯,东边有一个客厅”\n>)
如果inp在inv_命令中:
库存。打印_项()
“阅读手册”中的elif inp:
如果inventory.contain_项目:
打印(“”)
打印(“”)
打印(“”)
elif inp in up:
阁楼
东部的elif inp:
客厅
“退出窗口”中的elif inp:
后屋
elif inp==“打开的行李”:
打印(“”)
elif inp==“携带行李”:
打印(“”)
#这是房子的阁楼
def attic():
当游戏仍在运行时:
如果刀=0:
如果指示灯==0:
inp=input(“你在阁楼上,你可以看到一盏灯和一把刀。你看到血迹和向下的楼梯”\n>)
其他:
inp=input(“你在阁楼上,你可以看到一把刀。你看到血迹和向下的楼梯”\n>)
elif刀==1:
如果指示灯==0:
inp=input(“你在阁楼上,你可以看到一盏灯。你看到血迹和向下的楼梯”\n>)
其他:
inp=input(“你在阁楼上。你看到血迹和楼下的楼梯”\n>)
如果inp在inv_命令中:
库存。打印_项()
陶氏化学中的elif-inp
class Game:
def __init__(self):
self.mailbox_full = True
def take_leaflet(self):
if self.location != FRONT_HOUSE:
self.bogus()
return
if self.mailbox_full:
self.inventory.add_item(Item('Leaflet', 0, 0))
self.mailbox_full = False
self.msg('You take the leaflet')
else:
self.msg('There is no leaflet in the mailbox.')