2014-10-07 202 views
-2

我正在写这个程序,基本上是一个有限的计算器。我试图这样做,如果用户输入让我们说“电源”而不是数字1为所需的模式,它打印出“无效的选择”。如果他们试图写“Quadratics”而不是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") 

,但他们似乎并不管用。如果你能告诉我我做错了什么。谢谢。

回答

3

not不是功能。它是一个操作员。

正确的用法是把它表达之前:

if not (choice == 1): 

然而,在这种情况下,更好的做法是使用!=(不相等)来代替:

if choice != 1: