如何在Python中使用海象运算符时检查是否按下了回车键?
How to check if the Enter key was pressed while using the Walrus operator in Python?
我正在尝试使用 Walrus 运算符 :=
从用户那里获取输入,但是如果用户只输入 Enter 键作为 input
,那么 python 脚本将终止。我如何捕获此错误并确保用户不仅按下了 Enter 键?
有 this answer 但它不能使用海象运算符。
这段没有海象运算符的代码将成功检查不仅按下了 Enter 键:
while True:
answer = input("Please enter something: ")
if answer == "":
print("Invalid! Enter key was pressed.")
continue
else:
print("Enter wasn't pressed!")
# do something
如果用户只按下 Enter,那么整个脚本将终止。
while answer := input("Please enter something: "):
# if user pressed only `Enter` script will terminate. following will never run
if answer == "":
print("enter was pressed")
else:
print("Enter wasn't pressed!")
# do something
代码中发生了什么?
第 1 部分:
发生这种情况是因为此语句 answer := input("Please enter something: ")
获取输入值并将其分配给变量 answer
。如果您按 Enter
,则 answer
的值为空字符串。
第 2 部分:
while
循环计算值。由于该值为空字符串且空字符串求值为 false,因此循环退出
我们不能使用海象运算符来执行此操作,因为控件永远不会进入循环,而且您想要检查输入的字符串的值
您可以编写如下代码:
while (answer := input("Please enter something: ")) != 0:
# if user pressed only `Enter` script will terminate. following will never run
if answer == "":
print("enter was pressed")
else:
print("Enter wasn't pressed!")
# do something
这意味着 != 0 将始终满足。
你把赋值表达式放错了地方。您的原始循环是无限的,但您的第二个循环使用 answer
作为中断条件。
while True:
if not (answer := input("Type something: ")):
print("You didn't type anything before pressing Enter!")
continue
print("You typed:", answer)
此外,由于我们使用的是 continue
,因此不需要 else
子句。
在行动:
Type something: hello!
You typed: hello!
Type something:
You didn't type anything before pressing Enter!
Type something: hi!
You typed: hi!
但是,在这里使用海象运算符并没有真正的优势,所以我会避免使用它。
while True:
answer = input("Type something: ")
if not answer:
print("You didn't type anything before pressing Enter!")
continue
print("You typed:", answer)
我正在尝试使用 Walrus 运算符 :=
从用户那里获取输入,但是如果用户只输入 Enter 键作为 input
,那么 python 脚本将终止。我如何捕获此错误并确保用户不仅按下了 Enter 键?
有 this answer 但它不能使用海象运算符。
这段没有海象运算符的代码将成功检查不仅按下了 Enter 键:
while True:
answer = input("Please enter something: ")
if answer == "":
print("Invalid! Enter key was pressed.")
continue
else:
print("Enter wasn't pressed!")
# do something
如果用户只按下 Enter,那么整个脚本将终止。
while answer := input("Please enter something: "):
# if user pressed only `Enter` script will terminate. following will never run
if answer == "":
print("enter was pressed")
else:
print("Enter wasn't pressed!")
# do something
代码中发生了什么?
第 1 部分:
发生这种情况是因为此语句 answer := input("Please enter something: ")
获取输入值并将其分配给变量 answer
。如果您按 Enter
,则 answer
的值为空字符串。
第 2 部分:
while
循环计算值。由于该值为空字符串且空字符串求值为 false,因此循环退出
我们不能使用海象运算符来执行此操作,因为控件永远不会进入循环,而且您想要检查输入的字符串的值
您可以编写如下代码:
while (answer := input("Please enter something: ")) != 0:
# if user pressed only `Enter` script will terminate. following will never run
if answer == "":
print("enter was pressed")
else:
print("Enter wasn't pressed!")
# do something
这意味着 != 0 将始终满足。
你把赋值表达式放错了地方。您的原始循环是无限的,但您的第二个循环使用 answer
作为中断条件。
while True:
if not (answer := input("Type something: ")):
print("You didn't type anything before pressing Enter!")
continue
print("You typed:", answer)
此外,由于我们使用的是 continue
,因此不需要 else
子句。
在行动:
Type something: hello!
You typed: hello!
Type something:
You didn't type anything before pressing Enter!
Type something: hi!
You typed: hi!
但是,在这里使用海象运算符并没有真正的优势,所以我会避免使用它。
while True:
answer = input("Type something: ")
if not answer:
print("You didn't type anything before pressing Enter!")
continue
print("You typed:", answer)