2017-11-17 4 views
0
#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() 

私の恐ろしいことを言い訳これは私の最初のプロジェクトであり、私はそれが最初にうまく動作します。Pythonは私のテキストアドベンチャーで私のdefを超えて何も印刷しません

また、これはスタックオーバーフローに関する私の最初の投稿です。

私の問題は、私が必要としていたすべてのことを完璧に処理したことですが、その後詳細を追加すると、後でそれを行うことにしました。最初の部分だけを実行すると、最初の部分は "def choose_room():"のビットを完全に無視します。

原因を見つけるのに役立つかもしれないことを私が考えることができます。次回は何かを変更する前にファイルのコピーを作成して、これが再び起こらないようにします。

誰も問題が見つからない場合は、プログラムを最初から再作成しようとします。

+3

修正するための字下げがあるようです:-) Stack Overflowへようこそ! – Mangohero1

+0

@Mangohero1は正しいです。あなたのインデントを修正した後もあなたのコードを問題なく走らせました。具体的には、 'def choose_room'の後に全てがインデントされてその関数の一部であることを示します(最後に' choose_room'を呼び出す以外) – Nathan

+0

トリビアルですが、 'choose_room'の中で' choose_room() 'を呼ぶべきではありませんこの種の再帰が問題を引き起こす可能性があるからです。 –

答えて

0

インデントはシンタックスの一部です。インデントが不適切にインデントされた場合、コードは期待通りに動作しません。

修正しインデント:

#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

あなたは機能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

+0

彼はそれをファイルの最後の行と呼びます。 – Nathan

関連する問題