【发布时间】:2020-08-26 08:13:06
【问题描述】:
我正在尝试使用 python 在命令行中创建一个登录面板。所以如果用户没有插入任何username,光标应该保持在同一行但我应该在下一行得到一个错误。
我试过了:
while True:
input1 = input('username: ')
if len(input1) == 0:
print("Sorry, your response was not loud enough.")
continue
input2 = stdiomask.getpass('pasword: ')
if len(input2) == 0:
print("Sorry, your response was not loud enough.")
continue
input3 = stdiomask.getpass('cnf password: ')
if len(input3) == 0:
print("Sorry, your response was not loud enough.")
continue
但由于我使用的是while 循环,所以如果我没有插入密码,我必须再次插入用户名,如果我不插入用户名,我也不想要,用户名会在下一行提示显示错误后。那么有没有办法处理这些情况呢?
类似这样的:
F:\new_file\file> python main.py
? username: # cursor remains here until a username is inserted
> Invalid input # error is prmpted on next line
【问题讨论】:
-
尝试使用递归,贴出示例代码
标签: python python-3.x list input command-line-interface