Els*_*lsa 5 python sys python-3.x
好的,这是我的代码的一部分(我已经导入了sys)
if __name__ == '__main__':
MyCaesarCipher = CaesarCipher() #MyCaesarCipher IS a CaesarCipher()
if len(sys.argv) >1:
#what will it check?
Done = False
while not Done:
print('C Clear All')
print('L Load Encrypted File')
print('R Read Decrypted File')
print('S Store Encrypted File')
print('W Write Decrypted File')
print('O Output Encrypted Text')
print('P Print Decrypted Text')
print('E Encrypt Decrypted Text')
print('D Decrypted Encrypted Text')
print('Q Quit')
print('----------------')
print('Enter Choice>')
Run Code Online (Sandbox Code Playgroud)
所以我要做的是,如果命令行长度大于1,则程序将作为脚本运行。
这是指令:
如果未输入命令行参数,则脚本将进入菜单模式。如果在脚本运行期间提供了多个命令行参数(脚本名称以外的任何参数),它将进入单一运行模式。
我不知道这意味着什么。
什么是sys.arvg:
传递给Python脚本的命令行参数列表。argv[0]是脚本名称。
演示:文件名:1.py
import sys
if __name__=="__main__":
print "command arguments:", sys.argv
Run Code Online (Sandbox Code Playgroud)
输出:
$ python 1.py arg1 arg2
command arguments: ['1.py', 'arg1', 'arg2']
$ python 1.py
command arguments: ['1.py']
Run Code Online (Sandbox Code Playgroud)
您的问题是,我们必须通过命令行参数和菜单来运行代码。
当用户从命令行提供“输入选择”时,请使用提供的值进行下一步。
如果用户未从命令行提供“输入选择”,则要求用户从菜单中输入“选择输入”。
演示:
档案名称:1.py
import sys
if __name__ == '__main__':
try:
arg_command = sys.argv[1]
except IndexError:
arg_command = ""
Done = False
while not Done:
if arg_command=="":
print('\nMenu')
print('C Clear All')
print('L Load Encrypted File')
print('Q Quit')
print('----------------')
print('Enter Choice>')
command = raw_input('Enter Selection> ').strip()[0].upper()
else:
command = arg_command
#- set arg value to empty to run Menu option again.
arg_command = ""
if command == 'C':
print "In Clear All event."
elif command == 'L':
print "In Clear All event."
elif command == "Q":
break
else:
print "Wrong Selection."
Run Code Online (Sandbox Code Playgroud)
输出:
输入从命令行给出的选择:
$ python 1.py C
In Clear All event.
Menu
C Clear All
L Load Encrypted File
Q Quit
----------------
Enter Choice>
Enter Selection> q
$
Run Code Online (Sandbox Code Playgroud)
没有命令行参数。
$ python 1.py
Menu
C Clear All
L Load Encrypted File
Q Quit
----------------
Enter Choice>
Enter Selection> l
In Clear All event.
Menu
C Clear All
L Load Encrypted File
Q Quit
----------------
Enter Choice>
Enter Selection> q
$
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24266 次 |
| 最近记录: |