2016-06-26 27 views
-5

Ich weiß, dass diese Frage schon einmal gestellt wurde, aber sie scheint nicht für mich zu funktionieren. Ich möchte, wenn mir jemand sagen könnte, warum diese else-Anweisung mir einen Syntaxfehler gegeben hat.Kann mir jemand sagen, warum diese else-Anweisung mir einen Syntaxfehler in Python gibt

EDIT: Sorry, ich habe jetzt in der ganzen Code eingefügt, und welche Fehler ich erhalte. Tut mir leid, dass ich in meiner Frage vage oder inkonsequent bin.

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() 

Dies ist meine Fehlermeldung, wenn ich versuche, und es im Terminal laufen:

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

Sie müssen den Körper der 'entrance' Funktion einrücken. – edwinksl

+0

@edwinksl Wenn das der Fall wäre, wäre der Fehler in Zeile 2, nicht im 'else'. – SethMMorton

+1

Ich kopierte den Code und führte ihn erfolgreich mit Python2.7 auf meinem Computer aus (nachdem ich den Körper von 'entrance' eingerückt hatte). Sie sollten expliziter mit Ihrem Fehler sein, indem Sie das Traceback als Teil der Frage hinzufügen. – SethMMorton

Antwort

2

Sie sind am ehesten Misch Tabs mit Leerzeichen jetzt, dass Sie die Frage nach dem Einrücken Aussehen bearbeitet haben richtig und es funktioniert in der Tat wie vorgesehen mit dem, was Sie gepostet haben.

So ist die einzige andere Sache ist Ihre eigentliche Datei, die Sie gemischt Tabulatoren und Leerzeichen Ausführung hat so die inden falsch ist. Eine gute IDE wie PyCharm wird Ihnen dies zeigen und Sie davor warnen.

-1

Dies funktioniert für mich:

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() 
Verwandte Themen