python end of statement_17个新手常见Python运行时错误
12)?Trying to use a Python keyword for a variable name. (Causes “SyntaxError: invalid syntax”)
The?Python keywords (also called reserved words)?cannot be used for variable names. This happens with code like:
class = 'algebra'
The Python 3 keywords are:?and,?as,?assert,?break,?class,?continue,?def,?del,?elif,?else,?except,False,?finally,?for,?from,?global,?if,?import,?in,?is,?lambda,?None,?nonlocal,?not,?or,?pass,?raise,return,?True,?try,?while,?with,?yield
13)?Using an augmented assignment operator on a new variable. (Causes “NameError: name 'foobar' is not defined”)
Do not assume that variables start off with a value such as?0?or the blank string. A statement with an augmented operator like?spam += 1?is equivalent to?spam = spam + 1. This means that there must be a value in?spam?to begin with.
This error happens with code like this:
spam = 0
spam += 42
eggs += 42
14)?Using a local variable (with the same name as a global variable) in a function before assigning the local variable. (Causes “UnboundLocalError: local variable 'foobar' referenced before assignment”)
Using a local variable in a function that has the same name as a global variable is tricky. The rule is: if a variable in a function is ever assigned something, it is always a local variable when used inside that function. Otherwise, it is the global variable inside that function.
This means you cannot use it as a global variable in the function before assigning it.
This error happens with code like this:
someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
總結
以上是生活随笔為你收集整理的python end of statement_17个新手常见Python运行时错误的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 网站上传大小限制吗,配置PHP程
- 下一篇: python求第n个质数_找到第n个质数