#Asks player for their name and whether they wish to enter or not
character_name = input(" Welcome to The Tenabris Manor, what is your
name?:")
print("")
print(" The towering gates stand before you to the large Manor, do you
enter?")
print("")
inp = ""
while inp != "enter" and inp != "leave":
inp = input(" enter enter or leave: ")
if inp != "enter" and inp != "leave":
print(" You must type enter or leave")
if inp == "enter":
print(" You push with all your might on the large gates, it swings open with
a loud clunk.")
if inp == "leave":
print("")
print(" You turn around and go back, probably for the best.")
print("")
print(" Your character", character_name, "turned back and never returned.")
input(" Press enter to exit")
SystemExit("")
def choose_room():
#key = False is so the person does not have the key until going upstairs
global key
key = False
while True:
print("")
print(" Bookshelfs line the walls, a staircase is to the left and a door
is straight ahead.")
print("")
print(" Type 'a' to: Go up the stairs")
print(" Type 'b' to: To go through the door")
print(" Type 'c' to: To check the bookshelfs")
ans = input("")
if ans=='a':
print(" You walk up the creaking stairs")
print(" At the top of the spiral staircase is a small observatory.")
print(" Looking around on some of stacks of books littering the room
you")
print(" find a small key!")
key = True
continue
elif ans=='b':
#The door detects whether the key is True or not/they have the key or not.
if key == True:
print(" You open the door with the small key.")
elif key == False:
print(" The door is locked, you will need a key to go through
it.")
continue
return
choose_room()
else:
print("The door is locked, you will need a key to go through
it.")
continue
return
choose_room()
else:
ans == 'c'
print(" You look through the thousands of books.")
print(" None of them interest you.")
continue
return
choose_room()
请原谅我可怕的编码,这是我的第一个项目,我很惊讶它首先起作用。
这也是我关于堆栈溢出的第一篇文章。
我的问题是这段代码完美地完成了我需要的所有内容,但是当我去添加更多细节时,我决定稍后这样做并删除我“添加”的所有内容现在代码无法运行除了select select_room()之前的第一部分:
当我运行它时没有出现错误信息,只是第一部分然后忽略了“def choose_room():”的位。
嗯,我能想到的就是添加它可能有助于找到原因。下次我在更改任何内容之前复制文件,这样就不会再发生了。
如果没有人能找到问题所在,我会尝试从头开始重新制作程序。
答案 0 :(得分:0)
在Python中,缩进是语法的一部分,因此如果你有不正确的缩进信息,你的代码就不会像你期望的那样运行。
更正缩进:
#Asks player for their name and whether they wish to enter or not
character_name = input(" Welcome to The Tenabris Manor, what is your name?:")
print("")
print(" The towering gates stand before you to the large Manor, do you enter?")
print("")
inp = ""
while inp != "enter" and inp != "leave":
inp = input(" enter enter or leave: ")
if inp != "enter" and inp != "leave":
print(" You must type enter or leave")
if inp == "enter":
print(" You push with all your might on the large gates, it swings open with a loud clunk.")
if inp == "leave":
print("")
print(" You turn around and go back, probably for the best.")
print("")
print(" Your character", character_name, "turned back and never returned.")
input(" Press enter to exit")
SystemExit("")
def choose_room():
#key = False is so the person does not have the key until going upstairs
global key
key = False
while True:
print("")
print(" Bookshelfs line the walls, a staircase is to the left and a door is straight ahead.")
print("")
print(" Type 'a' to: Go up the stairs")
print(" Type 'b' to: To go through the door")
print(" Type 'c' to: To check the bookshelfs")
ans = input("")
if ans=='a':
print(" You walk up the creaking stairs")
print(" At the top of the spiral staircase is a small observatory.")
print(" Looking around on some of stacks of books littering the room you")
print(" find a small key!")
key = True
continue
elif ans=='b':
#The door detects whether the key is True or not/they have the key or not.
if key == True:
print(" You open the door with the small key.")
elif key == False:
print(" The door is locked, you will need a key to go through it.")
continue
return
choose_room()
else:
print("The door is locked, you will need a key to go through it.")
continue
return
choose_room()
else:
ans == 'c'
print(" You look through the thousands of books.")
print(" None of them interest you.")
continue
return
choose_room()
答案 1 :(得分:-1)
您定义了函数choose_room()
但从未使用/调用它。它会像这样。
#...
if inp == "enter":
print(" You push with all your might on the large gates, it swings open with a loud clunk.")
choose_room()
#...
确保在调用之前定义该函数。将def choose_room():...
置于脚本顶部或实际choose_room