python - 在 Python 中遇到嵌套 if-eilf-else 语句的问题
问题描述
我遇到了在 repl.it 上运行的 Python 中的嵌套 if 语句的问题,并且希望帮助确定我做错了什么,或者是否有一种很好的方法可以重新编写整个部分。
背景和当前目标: 我正在尝试制作一个可以与各种不同程序一起使用的通用设置模块。我对问题块的目标是能够检测何时将变量设置为特定字符串(通过字符串拆分器)以识别命令,然后检查参数(另一个变量)。最后,无论命令做什么都会被执行。
问题 Python/repl.it 将不接受我的嵌套 if 语句。
问题块:
elif(Command=="debug"):
print("Not Fully Implemented")
if(Args=='-E'):
DebugMode =="Enabled"
elif(Args=="D-"):
DebugMode == "False"
elif(Args=="-query"):
print(str(DebugMode)
else:
print("Argument Error. For a valid list of commands, type 'help'")
错误消息 (忽略颜色)
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
File "main.py", line 68
else:
^
SyntaxError: invalid syntax
到目前为止的完整程序:
#SettingsCommandLineUitily #Imports #Initization print("Starting Settings Utitlies... 000%",end="\r") from time import sleep import sys
#Initizatin Countdown
INIT_PERCENT=0
for i in range(97):
print("Loading Settings Utitlies... " + str(INIT_PERCENT) + "%", end="\r")
INIT_PERCENT += 1
sleep(0.015)
#Defing Variables
global Args2
global Command
global Args
global DebugMode
DebugMode = "Enabled"
print("Loading Settings Utitlies... 97%", end="\r")
#String Artist
#Splits Input comands into command, args, and args 2 via a space
def StringArtist(CommandIn):
#Import globals
global Command
global Args
global Args2
#Parseing
ParsedCommand = CommandIn.split(" ")
ParsedCommand.append("Null")
ParsedCommand.append("Null")
Command = ParsedCommand[0]
Args = ParsedCommand[1]
Args2 = ParsedCommand[2]
print("Loading Settings Utitlies... 98%", end="\r")
#Command Parser
#Interpets Comands and Arguments
def CommandParser(Command, Args, Args2):
if(Command=="help"):
print("Displaying Help... ",end="\n")
print("Command Args Function",end="\n")
print("exit Exits the helps utilty.",end="\n")
print("help Displays This Help Dialogue",end="\n")
print("vol -[0-100/+/-/+10/-10/query] Sets the volume as a percenage where zero is off",end="\n")
print("graphics -[(B/N/W/query) Sets the graphics qualitiy (Best/Normal/Worst)]",end="\n")
print("_ Not Implemented",end="\n")
print("debug -[E/D/query] Not Implemented",end="\n")
elif(Command=="exit"):
print("Exiting to Program...")
sys.exit(000)
elif(Command=="vol"):
print("Not Implemented")
elif(Command=="graphics"):
print("Not Implemented")
elif(Command=="_"):
print("Not Implemented")
elif(Command=="debug"):
print("Not Fully Implemented")
if(Args=='-E'):
DebugMode =="Enabled"
elif(Args=="D-"):
DebugMode == "False"
elif(Args=="-query"):
print(str(DebugMode)
else:
print("Argument Error. For a valid list of commands, type 'help'")
elif(Command=="Null"):
sys.exit("Input may not have a value of 'Null'. Program Error Code #201")
elif(Command==""):
print("To exit settings and return to your program type exit")
else:
print("Command not reconized. To refer to a refernce list, type 'help'.")
print("Loading Settings Utitlies... 99%", end="\r")
def Main():
print("\nReturing to Settings Utily...")
#Import global variables
global Command
global Args
global Args2
#Main
print("Enter Command Below or type help for help.")
#CommandIn=input()
StringArtist(input())
CommandParser(Command, Args, Args2)
#command = "NUll"
#Args = "Null"
#Args2= "Null"
print("Loading Settings Utitlies... COMPLETE", end="\n")
#Debuger
if(DebugMode=="Enabled"):
while True:
Main()
解决方案
之前的行else:
缺少右括号:
print(str(DebugMode)
推荐阅读
- php - PHP 警告中的 mysqli 问题:mysqli_stmt_prepare() 需要参数 1
- java - 如何防止错误 java.lang.IllegalStateException: Transaction already active, in Hibernate
- python - 在 Python 的 re.compile 中加入特殊字符
- git - 如何在对 vps 服务器执行 git push/pull 时禁用询问密码?
- filenames - ImageFileError:无法计算出“.nii”的文件类型
- c++ - 为什么这个while循环条件保持真实?
- node.js - 在 npm install 上出现错误,因为 npm ERR cb() 从未调用过
- amazon-s3 - AWS Glue 作业错误:调用 o82.parquet 时出错。未找到
- ruby-on-rails - 如何在 Rails 的数据表中显示名称而不是 user_id
- html - asp.net 下拉列表未从新创建的 html 下拉列表中更新