【问题标题】:Python Operators and If Condition [closed]Python 运算符和 If 条件
【发布时间】:2019-07-04 23:03:01
【问题描述】:

我的代码:

answer = str(raw_input("Is the information correct? Enter Y for yes or N for no"))
if answer == "y" or answer == "Y":
  print("this will do the calculation")
else:
  exit()

这种方式与OR运算符一起使用是否正确?

【问题讨论】:

  • 你可以使用if answer.lower() == 'y':if answer in ('y', 'Y'):
  • 是的,这是正确的方法。
  • 如果是错误的方式......你会得到不起作用的行为或堆栈跟踪的错误消息。你有得到吗?如果不是 - 为什么要问?这是教程 - 材料:docs.python.org/3/tutorial/controlflow.html#if-statements - 这不是 SO 的问题。
  • 完全不相关,但raw_input() 将始终返回一个字符串。

标签: python string if-statement operator-keyword


【解决方案1】:

是的,这是 OR 运算符的正确使用,您可以避免使用 OR 使输入变为小写或大写,但据我了解,您对有效的 OR 语法更感兴趣。

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2019-10-20
    • 2023-03-03
    • 2017-03-21
    • 2011-06-04
    • 2013-05-04
    相关资源
    最近更新 更多