首页 > 解决方案 > 创建一个默认情况下也可以使用的选项

问题描述

我有一个程序,默认情况下应该获取文本的 50 个字符,如果我们想获取更少的字符(例如,40、30、20 或 10),我们应该在命令行中指定它。我不知道如何默认设置 50 个字符而不在命令行中编写任何内容。

我的代码:

import operator 

option = "" 
option = sys.argv[5]

if "" in option: #this doesn't work
    text = text[:50]
elif "40" in option:
    text = text[:40]
elif "30" in option:
    text = text[:30]
elif "20" in option:
    text = text[:20]
elif "10" in option:
    text = text[:10]
else:
    print("Please, choose the length from 50, 40, 30, 20 or 10 characters and write it down in a command line")

将感谢任何提示/提示!

标签: pythonoptions

解决方案


推荐阅读