python中用于抽认卡游戏的记分函数

python中用于抽认卡游戏的记分函数,python,Python,我正在尝试将一个非常简单的分数函数添加到一个非常简单的抽认卡游戏中,但我无法让游戏记住包含分数的变量的值(它总是将其重置为0)。分数显然取决于用户的诚实程度(这很好),用户在猜单词时必须按“Y” from random import * def add_score(): pos_score = 0 score = input("Press Y if you got the correct word or N if you got it wrong!" ) if

我正在尝试将一个非常简单的分数函数添加到一个非常简单的抽认卡游戏中,但我无法让游戏记住包含分数的变量的值(它总是将其重置为0)。分数显然取决于用户的诚实程度(这很好),用户在猜单词时必须按“Y”

    from random import *

def add_score():
    pos_score = 0
    score = input("Press Y if you got the correct word or N if you got it wrong!" )
    if score == 'Y':
        pos_score += 1
    print(pos_score)

def show_flashcard():
    """ Show the user a random key and ask them
        to define it. Show the definition
        when the user presses return.    
    """
    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])

def add_flashcard():
    """ This function allows the user to add a new
        word and related value to the glossary. It will
        be activated when pressing the "a" button.
    """    
    key = input("Enter the new word: ")
    value = input("Enter the definition: ")

    glossary[key] = value
    print("New entry added to glossary.")




# Set up the glossary

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

# The interactive loop

exit = False
while not exit:
    user_input = input('Enter s to show a flashcard, a to add a new card. or q to quit: ')
    if user_input == 'q':
        exit = True
    elif user_input == 's':
        show_flashcard()
        add_score()
    elif user_input == 'a':
        add_flashcard()
    else:
        print('You need to enter either q, a or s.')
一些注意事项: 我知道现在代码中只实现了正分数,但我认为最好是一步一步地进行,并先让它工作

问题 在
def add_score()
中,每次都将变量初始化为
0
。此外,它是一个局部变量,这意味着您只能从函数
add_score()
中引用它。这意味着每次退出该函数时,该变量都会被完全删除

解决方案 您需要将其设置为全局变量,即在游戏开始时将其初始化为0,并在您的函数之外。然后在
add_score
中,您只需引用全局变量并增加它,而无需每次初始化它:

from random import *

def add_score():
    score = input("Press Y if you got the correct word or N if you got it wrong!" )
    if score == 'Y':
        global pos_score
        pos_score += 1
    print(pos_score)

# Set up the glossary

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

# The interactive loop
pos_score = 0 #NOTE you initialise it here as a global variable
exit = False
while not exit:
    user_input = input('Enter s to show a flashcard, a to add a new card. or q to quit: ')
    if user_input == 'q':
        exit = True
    elif user_input == 's':
        show_flashcard()
        add_score()
    elif user_input == 'a':
        add_flashcard()
    else:
        print('You need to enter either q, a or s.')
注意,我跳过了不相关的函数。然而,通常像这样改变变量的范围被认为是不好的做法。您应该做的是创建一个类(本例中有点过于复杂),或者从
add\u score
返回一个要添加的值,并将该值添加到主循环中。这就是代码:

from random import *

def add_score():
    score = input("Press Y if you got the correct word or N if you got it wrong!" )
    if score == 'Y':
        #global pos_score
        #pos_score += 1
        #print(pos_score)
        return 1
    return 0

def show_flashcard():
    """ Show the user a random key and ask them
        to define it. Show the definition
        when the user presses return.    
    """
    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])

def add_flashcard():
    """ This function allows the user to add a new
        word and related value to the glossary. It will
        be activated when pressing the "a" button.
    """    
    key = input("Enter the new word: ")
    value = input("Enter the definition: ")

    glossary[key] = value
    print("New entry added to glossary.")

# Set up the glossary

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

# The interactive loop
pos_score = 0 #NOTE you initialise it here as a global variable
exit = False
while not exit:
    user_input = input('Enter s to show a flashcard, a to add a new card. or q to quit: ')
    if user_input == 'q':
        exit = True
    elif user_input == 's':
        show_flashcard()
        pos_score += add_score()
        print(pos_score)
    elif user_input == 'a':
        add_flashcard()
    else:
        print('You need to enter either q, a or s.')
问题 在
def add_score()
中,每次都将变量初始化为
0
。此外,它是一个局部变量,这意味着您只能从函数
add_score()
中引用它。这意味着每次退出该函数时,该变量都会被完全删除

解决方案 您需要将其设置为全局变量,即在游戏开始时将其初始化为0,并在您的函数之外。然后在
add_score
中,您只需引用全局变量并增加它,而无需每次初始化它:

from random import *

def add_score():
    score = input("Press Y if you got the correct word or N if you got it wrong!" )
    if score == 'Y':
        global pos_score
        pos_score += 1
    print(pos_score)

# Set up the glossary

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

# The interactive loop
pos_score = 0 #NOTE you initialise it here as a global variable
exit = False
while not exit:
    user_input = input('Enter s to show a flashcard, a to add a new card. or q to quit: ')
    if user_input == 'q':
        exit = True
    elif user_input == 's':
        show_flashcard()
        add_score()
    elif user_input == 'a':
        add_flashcard()
    else:
        print('You need to enter either q, a or s.')
注意,我跳过了不相关的函数。然而,通常像这样改变变量的范围被认为是不好的做法。您应该做的是创建一个类(本例中有点过于复杂),或者从
add\u score
返回一个要添加的值,并将该值添加到主循环中。这就是代码:

from random import *

def add_score():
    score = input("Press Y if you got the correct word or N if you got it wrong!" )
    if score == 'Y':
        #global pos_score
        #pos_score += 1
        #print(pos_score)
        return 1
    return 0

def show_flashcard():
    """ Show the user a random key and ask them
        to define it. Show the definition
        when the user presses return.    
    """
    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])

def add_flashcard():
    """ This function allows the user to add a new
        word and related value to the glossary. It will
        be activated when pressing the "a" button.
    """    
    key = input("Enter the new word: ")
    value = input("Enter the definition: ")

    glossary[key] = value
    print("New entry added to glossary.")

# Set up the glossary

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

# The interactive loop
pos_score = 0 #NOTE you initialise it here as a global variable
exit = False
while not exit:
    user_input = input('Enter s to show a flashcard, a to add a new card. or q to quit: ')
    if user_input == 'q':
        exit = True
    elif user_input == 's':
        show_flashcard()
        pos_score += add_score()
        print(pos_score)
    elif user_input == 'a':
        add_flashcard()
    else:
        print('You need to enter either q, a or s.')

只是一个旁注,但可能更容易去掉带有
exit=False
的行,然后说
,而True:
,然后如果用户输入='q':则可以
打断
。只是旁注,但是,如果用户输入='q':您可以
中断
,那么可能更容易去掉带有
exit=False
的行,说
,而说True:
,然后说