你如何跟踪python中的全局变量

时间:2016-07-03 04:59:10

标签: python python-3.x

我想跟踪变量TOTAL_TRITOTAL_TRI包含游戏中正确回答的问题数。我需要保存该值,并在调用statistics时将其传递给函数statistics。基本上,玩家将玩游戏py_gameTOTAL_TRI将保持他们正确的问题数量,当玩家调用函数statistics时,它将显示他们的问题数量对了吗?我已经玩了一段时间没有取得任何重大进展。有什么想法吗?

P.S。 菜单中的其他游戏尚未实现,但他们会做同样的游戏 - 保存正确数量的问题 - 让玩家调用statistics类似的东西。

尝试使用全局变量更新:

#py_game------------------------------------------------------------------------
import random
from random import choice
from random import randint
TOTAL_TRI = 0
def py_game():
    for k in range (1,2):
        print('\nPractice Problem', k, 'of 2')
        min_pyramid_size = 3
        max_pyramid_size = 5
        total_chars = 0
        num_rows = random.randint(min_pyramid_size, max_pyramid_size)
        for i in range(num_rows):
            x = ''.join(str(random.choice('*%')) for j in range(2*i+1))
            print(' ' * (num_rows - i) + x)
            total_chars = total_chars + x.count('%')
        try:
            user_answer = int(input('Enter the number of % characters' + \
                                    ' in the pyramid: '))
        except:
                user_answer = print()
        if user_answer == total_chars:
            print('You are correct!')
        else:
            print("Sorry that's not the correct answer")
    points = 0
    global TOTAL_TRI 
    for k in range (1,2):
        print('\nProblem', k, 'of 10')
        min_pyramid_size = 3
        max_pyramid_size = 5
        total_chars = 0
        num_rows = random.randint(min_pyramid_size, max_pyramid_size)
        for i in range(num_rows):
            x = ''.join(str(random.choice('*%')) for j in range(2*i+1))
            print(' ' * (num_rows - i) + x)
            total_chars = total_chars + x.count('%')
        try:
            user_answer = int(input('Enter the number of % characters' + \
                                    ' in the pyramid: '))
        except:
                user_answer = print()
        if user_answer == total_chars:
            print('You are correct!')
            points +=1
        else:
            print("Sorry that's not the correct answer")
        TOTAL_TRI = points


#------------------------------------------------------------------------------
def statistics(points):

    print('\nPyramid Game---------------------------')
    incorrect = 10 - (points)
    print ('You answered', points, 'questions correctly')
    print ('You answered', incorrect, 'questions incorrectly')



#Main Menu--------------------------------------------------------------------------

def main_menu(): 
    calculation_game = print("Enter 1 for the game 'Calculation'")
    bin_reader = print("Enter 2 for the game 'Binary Reader'")
    trifacto_game = print("Enter 3 for the game 'Trifacto'")
    statistics = print("Enter 4 to view your statistics")
    display_data = print("Enter 5 to display data")
    save_game = print("Enter 5 to save your progress")
    user_input = int(input('Make your selection: '))
    if user_input == 1:
        calculation()
    if user_input == 2:
        binary_reader()
    if user_input == 3:
        py_game()
    if user_input == 4:
        statistics(TOTAL_TRI)
    if user_input == 5:
        save_game()
    if user_input != 1 or 2 or 3 or 4 or 5:
        print('invalid input')
        print('\n')
        main_menu()



main_menu()

1 个答案:

答案 0 :(得分:1)

在公共范围内声明变量TOTAL_TRI = 0。只要您想在函数中访问它,请使用global关键字:

TOTAL_TRI = 0

def some_function(...):
    global TOTAL_TRI
    ... # do something

您仍然可以在不使用global关键字的情况下执行此操作,但可以降低声明具有隐藏全局变量的同名的局部变量的风险,具体取决于您执行的操作。

编辑:我还应该提一下,你可以在函数中声明变量 ,如果你希望该变量在所有其他函数中可见,你应该在同一个函数中声明它是全局的它的定义也是如此。