0 python
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")
Run Code Online (Sandbox Code Playgroud)
你忘了关闭括号:
print('''do you wish to access this network''')
VAL= int(input("to entre please punch in the pass word: ")) # here!
if VAL is 2214:
Run Code Online (Sandbox Code Playgroud)
is
如果你想比较相等,我还建议避免使用运算符.
的is
运算符比较的身份(即,它是相同的目的,在相同的存储器位置),而==
比较是否相等(即,这些目的,根据它们的语义,可以被认为是相等的).
使用is
测试相等仅适用于在范围内的整数[-5, 256]
,由于实现细节(即这些数字缓存).所有其他数字都失败了.
为了突出显示冒号突出显示的原因而不是if
自身:
请记住,在python中,您可以将括号内的每个表达式括起来,以便将它写在多行中,但是您不能将语句放在括号内.语句和表达之间有明显的区别.
语句包括环语句(for
,while
,break
,continue
,else
),则if-elif-else
,try-except-finally
,with
,分配name = value
,函数和类等定义
表达式是一切:a + b
,object.method()
,function_call()
...
在您的特定示例中,解析器会看到该行:
VAL= int(input("to entre please punch in the pass word: ")
Run Code Online (Sandbox Code Playgroud)
这是一个任务说明.它指定右侧表达式VAL
的值.因此它解析表达式,因为在这一行上没有右括号,它继续在下一行解析.但在下一行它发现: int(input(...) ...
if VAL is 2214:
Run Code Online (Sandbox Code Playgroud)
这是一个语句,因为:
末尾有冒号,表达式不能包含语句.这也是为什么你不能做的事情if (a=100) < 50:
,即在一个条件内进行分配.
它if VAL is 2214
本身不是错误,因为还存在一个表达式if
(实际上称为条件表达式).例如,以下是有效的 python代码:
VAL = int(input("prompt ")
if n % 2 == 0 else input("different prompt "))
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,您必须指定两者if
并且else
条件表达式中没有冒号.
归档时间: |
|
查看次数: |
170 次 |
最近记录: |