如何在不多次引用变量 'score' 的情况下创建问题函数?
How do I create a question function without referencing the variable 'score' multiple times?
我一直在尝试在 python 中构建游戏。我有一个问题函数,它将 运行 来自变量的问题:
- 正确答案
- 回答1
- 回答2
- 回答3
- answer4
- 问题
- 和
answer
作为输入
这是函数的代码:
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
我该如何解决这个问题?
如果分数仅在问题函数中引用,请尝试在函数开始时为其分配值 0。
如果您需要在函数外引用它,请尝试在函数外为其赋值并传入。
代码:
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
添加分数的函数
您可以在 question()
函数之外使用另一个名为 addscore()
的函数,它会为分数加分并打印出所有必要的信息。
def addscore(score, add=0, subtract=0):
if add > 0:
print(f'Correct! Score + {add}')
elif subtract > 0:
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(f'Score - {subtract}')
score = score + add - subtract
print('Score = ', score)
if subtract > 0:
death.start()
print('░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄')
time.sleep(0.5)
print('░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀')
time.sleep(0.5)
print('░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀')
question()
return score
实施
之后,就可以将这个功能实现到原来的question()
功能中。有了这个,你可以加多一分。
例如
这将添加 5 个点并打印消息。
score = addscore(score, add=5)
或者这样减去3分。
score = addscore(score, subtract=3)
玩得开心!
我一直在尝试在 python 中构建游戏。我有一个问题函数,它将 运行 来自变量的问题:
- 正确答案
- 回答1
- 回答2
- 回答3
- answer4
- 问题
- 和
answer
作为输入 这是函数的代码:
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
我该如何解决这个问题?
如果分数仅在问题函数中引用,请尝试在函数开始时为其分配值 0。
如果您需要在函数外引用它,请尝试在函数外为其赋值并传入。
代码:
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
添加分数的函数
您可以在 question()
函数之外使用另一个名为 addscore()
的函数,它会为分数加分并打印出所有必要的信息。
def addscore(score, add=0, subtract=0):
if add > 0:
print(f'Correct! Score + {add}')
elif subtract > 0:
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(f'Score - {subtract}')
score = score + add - subtract
print('Score = ', score)
if subtract > 0:
death.start()
print('░▒█▀▀█░█▀▀▄░█▀▄▀█░█▀▀░░░▄▀▀▄░▄░░░▄░█▀▀░█▀▀▄')
time.sleep(0.5)
print('░▒█░▄▄░█▄▄█░█░▀░█░█▀▀░░░█░░█░░█▄█░░█▀▀░█▄▄▀')
time.sleep(0.5)
print('░▒█▄▄▀░▀░░▀░▀░░▒▀░▀▀▀░░░░▀▀░░░░▀░░░▀▀▀░▀░▀▀')
question()
return score
实施
之后,就可以将这个功能实现到原来的question()
功能中。有了这个,你可以加多一分。
例如
这将添加 5 个点并打印消息。
score = addscore(score, add=5)
或者这样减去3分。
score = addscore(score, subtract=3)
玩得开心!