2016-06-26 4 views
-5

私はこの質問が以前に尋ねられたことは知っていますが、私のために働いていないようです。このelse文がなぜ私に構文エラーを与えているのか誰かが私に教えてもらいたいと思います。誰かがこのelse文が私にPythonの文法エラーを与える理由を教えてもらえますか

編集:申し訳ありませんが、私は今、コード全体を貼り付けていますが、何のエラーが表示されていますか。私の質問であいまいであるか矛盾していることについては申し訳ありません。

from sys import argv 
from sys import exit 
from time import * 
from random import * 
import os,sys 
#importing argv and exit modules 

def start(): #defining different functions at the very start 
    next = raw_input("> ") 
    if next.lower() == "start": 
     entrance() 
    else: 
     print "I'm sorry, I don't understand this command" 
start() 
def clear_screen(): #will help clear the screen 
    os.system('cls' if os.name=='nt' else 'clear') 

def title(): #A simple title screen 
    print "CAVE OF SECRETS" 

def battery(): #A simple function to monitor battery life 
    LIFE = 10 

def dead(reason): #Death messages 
    print "You are dead because", reason 
    exit(0) 
############################################### 
#The 'Actual' Game begins here, in the entrance!! 
def entrance(): 
    print "You wake up in a dark cave. There isn't enough light to see anything." 
    print "Would you like to move forward anyway?" 
    next = raw_input("> ") 

    if next.lower() == "yes": 
     print "Your feet touch something upon the floor" 
     print "\nA FLASHLIGHT! And it still has full battery!" 
    elif next.lower() == "no": 
     print "So you just stand there....for days. And without any light or way back." 
     print "It was only a matter of time before you starved to death anyway. So you convince yourself to give up." 
     print "I guess that's easier then actually trying right?" 
     print "\nGAME OVER" 
     exit(0) 
    else: #THIS IS THE ONE GIVING ME ERRORS 
     print "I'm sorry, I don't understand this command" 
     entrance() 
############################################### 
clear_screen() 
title() 
battery() 
print "Welcome to the Cave of Secrets" 
print "What is your name Adventurer?" 
player_name = raw_input("> ") 

print "Beware this journey is not for the faint of heart, %s" % player_name 
print "Please type in START to begin the quest!" 
start() 

私がしようとすると、端末でそれを実行したときにこれは私のエラーメッセージです:

File "cave.py", line 43 
    else: 
    ^
SyntaxError: invalid syntax 
+3

あなたは 'entrance'関数の本体をインデントする必要があります。 – edwinksl

+0

@edwinkslこれが当てはまる場合、エラーは2行目であり、 'else'では発生しません。 – SethMMorton

+1

Python2.7を使ってコードをコピーして、コンピュータ上で正常に実行しました( 'entrance'の本文をインデントした後)。問題の一部としてトレースバックを追加することで、エラーの内容をより明確にする必要があります。 – SethMMorton

答えて

2

あなたが最も可能性の高いスペースと混合タブいる今、あなたが質問にインデントルックスを編集したことを正しいとそれは実際にあなたが投稿したものと意図どおりに動作します。

あなたの実際のファイルにはタブとスペースが混在しているため、インデントが正しくありません。 PyCharmのような良いIDEはあなたにこれを示し、それについて警告します。

-1

これが私の作品:

from sys import argv 
from sys import exit 
from time import * 
from random import * 
import os,sys 
#importing argv and exit modules 

#The 'Actual' Game begins here, in the entrance!! 
def entrance(): 
    print "You wake up in a dark cave. There isn't enough light to see anything." 
    print "Would you like to move forward anyway?" 
    next = raw_input("> ") 

    if next.lower() == "yes": 
     print "Your feet touch something upon the floor" 
     print "\nA FLASHLIGHT! And it still has full battery!" 
    elif next.lower() == "no": 
     print "So you just stand there....for days. And without any light or way back." 
     print "It was only a matter of time before you starved to death anyway. So you convince yourself to give up." 
     print "I guess that's easier then actually trying right?" 
     print "\nGAME OVER" 
     exit(0) 
    else: #THIS IS THE ONE GIVING ME ERRORS 
     print "I'm sorry, I don't understand this command" 
     entrance() 

###############################################  
def start(): #defining different functions at the very start 
    next = raw_input("> ") 
    if next.lower() == "start": 
     entrance() 
    else: 
     print "I'm sorry, I don't understand this command" 
start() 
def clear_screen(): #will help clear the screen 
    os.system('cls' if os.name=='nt' else 'clear') 

def title(): #A simple title screen 
    print "CAVE OF SECRETS" 

def battery(): #A simple function to monitor battery life 
    LIFE = 10 

def dead(reason): #Death messages 
    print "You are dead because", reason 
    exit(0) 

############################################### 
clear_screen() 
title() 
battery() 
print "Welcome to the Cave of Secrets" 
print "What is your name Adventurer?" 
player_name = raw_input("> ") 

print "Beware this journey is not for the faint of heart, %s" % player_name 
print "Please type in START to begin the quest!" 
start() 
関連する問題