"请问有人能告诉我为什么这个else语句在Python中给我一个语法错误?"

2024-09-27 09:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道以前有人问过这个问题,但他们似乎对我没用。我希望有人能告诉我为什么这个else语句给了我一个语法错误。在

编辑:对不起,我已经粘贴了整个代码,我得到了什么错误。很抱歉我的问题含糊不清或前后矛盾。在

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

这是我尝试在终端中运行时的错误消息:

^{pr2}$

Tags: thetonamefromimportdefexitscreen
2条回答

既然你已经编辑了这个问题,那么你很可能把制表符和空格混在一起了,缩进看起来是正确的,而且它确实与你所发布的内容一致。在

所以唯一的另一件事是你正在执行的实际文件有混合的制表符和空格,所以缩进是不正确的。像PyCharm这样的好IDE会向您展示并警告您。在

这对我有用:

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

相关问题 更多 >

    热门问题