我正在进行我的第一个python编程。我没有电脑编程经验。我在函数中使用全局变量时遇到问题。所以我重新定义了它们
这是我的程序的顶部,在这里我定义了全局变量
#Define Global Variables
segments = []
highscore = 0
currentscore = 0
delay = 0.2
level = 1
这里是我使用它们的函数之一。一切正常,但这被认为是坏习惯吗?我应该把它们作为论据传递出去吗?只是想知道这最好的方法是什么。我不想发布完整的代码,因为它只有200多行,而且运行良好
def gameover():
global highscore
global currentscore
global delay
global level
time.sleep(1)
if currentscore >= highscore:
highscore = currentscore
sethighscore()
currentscore = 0
head.goto(0,0)
head.direction = "stop"
for index in range(len(segments)):
x = 1000
y = 1000
segments[index].goto(x,y)
segments.clear()
delay = 0.2
level = 1
代码最佳实践(并非python独有)
对于案例过程参数,如果需要,返回值
相关问题 更多 >
编程相关推荐