【问题标题】:How to properly use the "not" operator?如何正确使用“非”运算符?
【发布时间】:2014-10-07 01:18:53
【问题描述】:

我正在编写这个程序,它基本上是一个有限的计算器。我正在尝试这样做,以便如果用户输入让我们说“电源”而不是所需模式的数字 1,它会打印出“无效选择”。如果他们尝试写“二次方”而不是 2 等等,其余部分也是如此。

#CALCULATOR
print("MY CALCULATOR")
print("1. Powers")
print("2. Quadratics")
print("3. Percents")
print("4. Basic Ops")
choice = int(input("Please enter your desired mode: "))
if choice == 1:
    base = int(input("Enter the base: "))
    exponent = int(input("Enter the exponent: "))
    power = base**exponent
if choice == 2:
    print("Please enter the values for A/B/C: ")
    a = int(input("A: "))
    b = int(input("B: "))
    c = int(input("C: "))

我试过了:

if choice not == 1:
    print("Invalid Selection")

if choice not 1:
    print("Invalid Selection")

但它们似乎不起作用。如果你能告诉我我做错了什么。谢谢你。

【问题讨论】:

    标签: python operator-keyword


    【解决方案1】:

    not 不是函数。它是一个运算符。

    正确的用法是放在表达式之前:

    if not (choice == 1):
    

    但是在这种情况下,最好改用!=(不等于):

    if choice != 1:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 2023-03-28
      • 2014-06-06
      • 2021-09-30
      • 1970-01-01
      相关资源
      最近更新 更多