在Python3中,我想使用一行来获取多个输入,并且如果用户仅给出一个输入,则算法将决定输入是否为负数。如果是,则算法停止;否则,算法停止。否则,该算法将循环以获取正确的输入。
我的代码:
location ~ \.php$
当我尝试代码时,Python返回
integer, string = input("Enter an integer and a word: ")
我尝试了“ try”和“ except”,但是我无法获得“ integer”输入。我该如何解决?
答案 0 :(得分:2)
[编辑] 为了一次获得两个输入,可以使用split()。就像下面的示例一样:
public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId == (CefMenuCommand)26501)
{
// custom logic
return true;
}
return false;
}
答案 1 :(得分:0)
我相信以下实现可以满足您的需求。
仅当用户提供单个输入(如果用户提供一个整数后跟非数字字符串)为负数时,程序才会退出。
userinput = []
while True:
userinput = input("Enter an integer and a word: ").split()
# exit the program if the only input is a negative number
if len(userinput) == 1:
try:
if int(userinput[0]) < 0:
break
except:
pass
# exit the program if a correct input was provided (integer followed by a non-numeric string)
elif len(userinput) == 2:
if userinput[0].isnumeric() and (not userinput[1].isnumeric()):
break