[英]Python 2.7 Control Flow
I'm currently have a little problem: I'm making a "command" for my bot, a little password generator, currently, I already have the command itself, it'll go like this 我目前有一个小问题:我正在为我的机器人制作一个“命令”,一个小的密码生成器,目前,我已经有了该命令本身,它将像这样
if Command.lower() == '!pwgen' #First message, the user calls the command
length = Argument[0] #The length he wants, Argument[0] == the length
import os, random, string
length = 13
chars = string.ascii_letters + string.digits + '!@#$%^&*()'
random.seed = (os.urandom(1024))
password = ''.join(random.choice(chars) for i in range(length))
messages.Main(password,xSock,BotID)
There's my problem, I wanna make sure that, if the argument is empty, I want the password generator to use a default digit, eg 12 so the length will be 12 if there's no length given by the user. 有一个问题,我想确保,如果参数为空,我希望密码生成器使用默认数字,例如12,因此如果用户未指定长度,则长度将为12。 Thanks.
谢谢。
Assuming Argument
is a list you could try to retrieve the value of the length and if the Argument
is empty ie raises an IndexError
give length
the desired default value: 假设
Argument
是一个列表,您可以尝试检索长度的值,并且如果Argument
为空,即引发IndexError
,则为length
所需的默认值:
try:
length = Argument[0]
except IndexError as error:
length = 12
To handle other types of wrong input eg negative values, None
values, using a simple if
statement or a ternary assignment should be enough. 要处理其他类型的错误输入,例如负值,
None
值,使用简单的if
语句或三元赋值就足够了。
根据您所拥有的,尝试:
length = Argument[0] if len(Argument)>0 and Argument[0] is not None else 12
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.