0
print('''do you wish to access this network''')
VAL= int(input("to entre please punch in the pass word: ")
if VAL is 2214 **:**
      print("welcome")
else:
      print("wrong password, please check retry")
4

4 回答 4

5

您忘记关闭括号:

print('''do you wish to access this network''')
VAL= int(input("to entre please punch in the pass word: "))   # here!
if VAL is 2214:

is如果您想比较是否相等,我还建议避免使用运算符。

is运算符比较身份即它是同一个对象,在同一个内存位置),同时==比较相等(即这些对象,根据它们的语义,可以被认为是相等的)。

由于实现细节(即这些数字被缓存)is,用于测试相等性仅适用于范围内的整数。它对所有其他数字都失败了。[-5, 256]


扩展一下为什么要突出显示冒号而不是冒号if本身:

请记住,在 python 中,您可以将每个表达式括在括号之间,以便将其写成多行,但您不能将语句放在括号内。语句表达式之间有明显的区别

语句包括循环语句( for, while, break, continue, else), the if-elif-else, try-except-finally, with, assignment name = value, 函数和类的定义等。

表达式就是其他一切:a + b, object.method(), function_call()...

在您的具体示例中,解析器看到以下行:

VAL= int(input("to entre please punch in the pass word: ")

这是一个赋值语句。它分配给右侧表达式VAL的值。因此它解析表达式,并且由于这一行没有右括号,它继续解析下一行。但在下一行它发现: int(input(...) ...

if VAL is 2214:

这是一个语句,因为:末尾有冒号,表达式不能包含 statements。这也是为什么你不能做类似的事情if (a=100) < 50:,即在条件内赋值。

if VAL is 2214本身不是错误,因为还存在-表达式if(实际上称为条件表达式)。例如以下是有效的python 代码:

VAL = int(input("prompt ")
if n % 2 == 0 else input("different prompt "))

但是,在这种情况下,您必须同时指定ifandelse并且条件表达式中没有冒号。

于 2013-09-05T07:34:39.013 回答
2

第一个错误是您忘记关闭),正如@Bakuriu 指出的那样:

VAL= int(input("to entre please punch in the pass word: "))

第二个错误是您is用于比较数字,这是比较数字的错误方法。is用于识别诸如a is None、 或之类的东西a is a。对于数字,它仅适用于以下的小数字256

>>> a = 10
>>> b = 10
>>>
>>> a is b
True
>>>
>>> a = 256
>>> b = 256
>>>
>>> a is b
True
>>>

但高于此数字它将返回False

>>> a = 257
>>> b = 257
>>>
>>> a is b
False
>>>

您应该始终==用于比较数字。

于 2013-09-05T07:49:05.283 回答
0

确保关闭此行的两个括号。

VAL= int(input("to entre please punch in the pass word: ")
于 2013-09-05T07:36:41.573 回答
0

看起来您正在尝试使用 python 3.x,我不知道您为什么要以:结束一行,它应该以 : 结束,就像这样。

print('''do you wish to access this network''')
VAL= int(input("to entre please punch in the pass word: "))
if VAL is 2214:
      print("welcome")
else:
      print("wrong password, please check retry")

请注意对第 3 行的更改。还要注意第 2 行缺少的括号 此外,请考虑通过字符串而不是 int 转换进行比较,因为如果用户输入非数字,它将引发异常以将其转换为 int。

我会使用以下内容。

VAL=input("to enter please punch in the password: ")
if VAL=='2214':
  print('welcome')
else:
  print("wrong password, please retry")

最后一点,上面的代码在 python 2.x 上运行是危险的,因为 input 执行输入的字符串而不是捕获字符串值。

于 2013-09-05T07:48:40.453 回答