【问题标题】:Changing global variable from within a function?从函数中更改全局变量?
【发布时间】:2015-04-26 00:15:49
【问题描述】:

只是为了练习,我正在尝试在 Python 中重新创建游戏Dudo(但我认为游戏知识不会对我的问题产生太大影响)。基本上,六个玩家有一定数量的骰子(以count1,count2等表示),我需要知道每种骰子的数量(多少个1,多少个2等)。我创建了一个变量 num1 (num1 = 0) 来表示 1 的个数,然后我做了一个函数来查找 1 的个数。

def find_num1():
    total = 0
    for i in range(you_count):
        if you[i] == 1:
            total = total + 1
    for i in range(count1):
        if enemy1[i] == 1:
            total = total + 1
    for i in range(count2):
        if enemy2[i] == 1:
            total = total + 1
    for i in range(count3):
        if enemy3[i] == 1:
            total = total + 1
    for i in range(count4):
        if enemy4[i] == 1:
            total = total + 1
    for i in range(count5):
        if enemy5[i] == 1:
            total = total + 1
    num1 = total
    print total

每个玩家都有一个包含六个数字的列表,代表他们拥有的每个骰子(enemy1、enemy2 等)。所以我使用一个循环遍历每个列表索引以找到数字 1,然后根据它更新总数。当我运行该功能时,它会正确打印总计。但是,如果我告诉它打印 num1(在函数之外),它会告诉我 0,这意味着我的函数没有正确地将 num1 更改为总计。有谁知道我做错了什么?

谢谢!

【问题讨论】:

  • global num1作为函数的第一行(Python 2)或nonlocal num1(Python 3)。
  • 谢谢!我使用的是 Python 3,但由于某种原因,它适用于全局而不是非本地......为什么?
  • global 在两个版本中都有效。 nonlocal 语句仅适用于 Python 3,它的含义有所不同。它用于修改非全局外部作用域中的变量,仅当您在其他函数中定义函数时才会出现。

标签: python function variables global


【解决方案1】:

告诉程序num1是一个全局变量

def find_num1():
    global num1 #Here
    total = 0
    for i in range(you_count):
        if you[i] == 1:
            total = total + 1
    for i in range(count1):
        if enemy1[i] == 1:
            total = total + 1
    for i in range(count2):
        if enemy2[i] == 1:
            total = total + 1
    for i in range(count3):
        if enemy3[i] == 1:
            total = total + 1
    for i in range(count4):
        if enemy4[i] == 1:
            total = total + 1
    for i in range(count5):
        if enemy5[i] == 1:
            total = total + 1
    num1 = total
    print total

【讨论】:

  • 这个有效!谢谢!只是想知道,我刚从 CodeAcademy 出来,这以前从来不是问题。有这样的功能吗?这是否意味着我可以重复使用变量的名称,只要它们位于不同的函数中?
  • Python 在scoping 上有很好的文档。
【解决方案2】:

随着您的程序变得更长,拥有过多的全局变量会使您的程序难以理解,当您发现错误时难以修复,并且当您想要添加功能时难以改进。幸运的是,有一种干净的方法可以将值从函数传递回主程序:return。如果您的顶级是foo = some_func(),并且some_func()return 5 结尾,那么在some_func() 完成后,5 将分配给foo。你可以改写如下:

def find_num1():
    total = 0
    for i in range(you_count):
        if you[i] == 1:
            total = total + 1
    for i in range(count1):
        if enemy1[i] == 1:
            total = total + 1
    for i in range(count2):
        if enemy2[i] == 1:
            total = total + 1
    for i in range(count3):
        if enemy3[i] == 1:
            total = total + 1
    for i in range(count4):
        if enemy4[i] == 1:
            total = total + 1
    for i in range(count5):
        if enemy5[i] == 1:
            total = total + 1
    return total

num1 = find_num1()
print(num1)

您甚至可以通过在函数中创建一个元组并在顶层解包该元组来返回多个值:

def sum_and_product(a, b):
    sum_value = a + b
    prod_value = a * b
    return (sum_value, prod_value)

(plusle, times) = sum_and_product(3, 5)
# the function returned (8, 15), and after unpacking,
# plusle is now 8 and times is now 15

另请参阅 Learn Python the Hard Way 中的 return examples

此答案中的代码是双重许可的:CC BY-SA 3.0MIT License as published by OSI

【讨论】:

  • 这就是我要写的。一个小的改进:值周围的括号 returned 和赋值左侧的括号是不必要的。您只需要使用括号来创建一个元组,否则它会模棱两可(例如在函数调用中)。如果您正在解压缩嵌套结构(例如 (a,b), c = [["foo", "bar"], "quux"]),则只需要在分配的左侧使用它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多