python - 如何在不多次引用变量“分数”的情况下创建问题函数?
问题描述
我一直在尝试在 python 中构建游戏。我有一个问题函数,它将从变量中运行一个问题:
- 正确答案
- 答案1
- 答案2
- 答案3
- 答案4
- 问题
- 并
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
我该如何解决这个问题?
解决方案
代码:
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
推荐阅读
- python - 使用与 OOXML 命名空间匹配的 XML 编写 Open Office XML(例如 docx)
- r - 如何捕获警告并仍然运行表达式
- asp.net-core-2.1 - ASP.NET Core Web API (2.1.4) 项目导致 HTTP 502.5 错误,错误代码为 0x80070005
- c++ - Qt 婚介服务器
- node.js - 可以监控节点 API 调用的响应吗?
- c# - 具有引用对象容器的只读属性的对象的 Newtonsoft json 反序列化
- r - 将自定义多元回归的输出从 R 导出到 Latex
- java - 当我从 CDI 支持 bean 调用 EJB Dao 时,返回的实体中的 @OneToMany 集合被清除并为空
- azure-powershell - 获取 VM 名称和 FQDN
- markdown - 注释掉表格行