首页 > 解决方案 > 如何在不多次引用变量“分数”的情况下创建问题函数?

问题描述

我一直在尝试在 python 中构建游戏。我有一个问题函数,它将从变量中运行一个问题:

    def question():
        print('question')
        print('A = ', answer1)
        print('B = ', answer2)
        print('C = ', answer3)
        print('D = ', answer4)
        answer = input('Remember, Case sensitive!\n')
    
        if answer == 'A':
            if correctanswer == 'A':
                print('Correct! Score +1')
                score = score + 1
                print('Score = ', score)
            else:
                print('Incorrect! The glowing device in my arm buzzed. The world around me started to pixelate. I felt a sharp zap in my arm and everything went black')
                print('Score - 1')
                score = score - 1
                print('Score = ', score)
                death.start()
                print('░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄')
                time.sleep(0.5)
                print('░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀')
                time.sleep(0.5)
                print('░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀')
                question()
        elif answer == 'B':
            if correctanswer == 'B':
                print('Correct! Score +1')
                score = score + 1
                print('Score = ', score)
            else:
                print('Incorrect! The glowing device in my arm buzzed. The world around me started to pixelate. I felt a sharp zap in my arm and everything went black')
                print('Score - 1')
                score = score - 1
                print('Score = ', score)
                death.start()
                print('░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄')
                time.sleep(0.5)
                print('░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀')
                time.sleep(0.5)
                print('░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀')
                question()
        elif answer == 'C':
            if correctanswer == 'C':
                print('Correct! Score +1')
                score = score + 1
                print('Score = ', score)
            else:
                print('Incorrect! The glowing device in my arm buzzed. The world around me started to pixelate. I felt a sharp zap in my arm and everything went black')
                print('Score - 1')
                score = score - 1
                print('Score = ', score)
                death.start()
                print('░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄')
                time.sleep(0.5)
                print('░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀')
                time.sleep(0.5)
                print('░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀')
                question()
        if answer == 'D':
            if correctanswer == 'D':
                print('Correct! Score +1')
                score = score + 1
                print('Score = ', score)
            else:
                print('Incorrect! The glowing device in my arm buzzed. The world around me started to pixelate. I felt a sharp zap in my arm and everything went black')
                print('Score - 1')
                score = score - 1
                print('Score = ', score)
                death.start()
                print('░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄')
                time.sleep(0.5)
                print('░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀')
                time.sleep(0.5)
                print('░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀')
                time.sleep(1)
                question()
        else:
            print('Invalid, try again')
            question()

如您所见,除了减去一分外,没有死亡处罚。我收到语法错误: UnboundLocalError: local variable 'score' referenced before assignment 我该如何解决这个问题?

标签: pythonsyntax-errorvariable-assignment

解决方案


代码:

import time
def question(answer,correctanswer,score):
    print('question')
    print('A = ', answer[0])
    print('B = ', answer[1])
    print('C = ', answer[2])
    print('D = ', answer[3])
    answer = input('Remember, Case sensitive!\n')

    if not answer in ["A","B","C","D"]:
        print('Invalid, try again')
        question(answer,correctanswer,score)

    if answer == correctanswer:
        print('Correct! Score +1')
        score = score + 1
        print('Score = ', score)
    else:
        print('Incorrect! The glowing device in my arm buzzed. The world around me started to pixelate. I felt a sharp zap in my arm and everything went black')
        print('Score - 1')
        score = score - 1
        print('Score = ', score)
        print('░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄')
        time.sleep(0.5)
        print('░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀')
        time.sleep(0.5)
        print('░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀')
        # question(nextanswer,nextcorrectanswer,score)

score = 0
question(["answer1","answer2","answer3","answer4"],"C",score)

结果:

question
A =  answer1
B =  answer2
C =  answer3
D =  answer4
Remember, Case sensitive!
A
Incorrect! The glowing device in my arm buzzed. The world around me started to pixelate. I felt a sharp zap in my arm and everything went black
Score - 1
Score =  -1
░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄
░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀
░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀

question
A =  answer1
B =  answer2
C =  answer3
D =  answer4
Remember, Case sensitive!
C
Correct! Score +1
Score =  1

推荐阅读