python - 从另一个函数获取数据
问题描述
但是,我有一个简短的问题,我正在尝试制作一个测验程序。我希望在整个程序中共享函数数据,
那么我该如何让 ask 函数 gen 从 getQuestion 中获取数据呢?
import random
All_questions = ["whats obamas last name ","Riclug is snygg ","Are traps gay "]
questions_Right = ["care","no","no"]
points = 0
tries = 3
ListNumber = len(All_questions)
def getQuestion():
question_number = random.randint(0, ListNumber - 1)
right_anwser = questions_Right[question_number]
Question = All_questions[question_number]
def ask(Question,right_anwser):
print("The question is: ")
anwser = input(Question+": ").casefold()
if anwser == right_anwser:
print("yes,", right_anwser,"was right\n")
All_questions.remove(Question)
questions_Right.remove(right_anwser)
else:
print("Sorry, but the answer was", right_anwser,"\n")
while True:
if ListNumber == 0:
print("Game over")
break
else:
print(ListNumber)
getQuestion()
ask()
print(All_questions)
解决方案
只需简单地使用return
:
def getQuestion():
# ... your code
return (Question, rightAnswer)
#... your code
Question, rightAnswer = getQuestion()
也许尝试使用字典,而不是问题和答案数组。那会更合适。
在继续进行问答游戏之前,我建议您进一步阅读并制作一些教程:
推荐阅读
- php - Accelo API - 返回带有任务列表的公司名称
- java - 有没有办法在java中制作一个随机数生成器,它更有可能落在一个数字上而不是另一个数字上?
- sql - 选择具有不同where子句的2列同一张表
- docker - 节点重启时延迟 Docker Swarm 副本重新调度
- python - 如何在一个图表中绘制多个文件?
- reactjs - React & Next.js — Bug: getting previous page's value from session storage
- apk - “app not installed” Apk 编辑器编辑清单 Android 存储位置
- javascript - 如何将回调函数返回的值赋给变量?从另一个问题无法理解。关于异步调用
- github - 如何从 GitHub 包中删除旧的快照工件
- javascript - 在 jsfiddle 中,它在纯 javascript 中工作,但是 - 当我在我的代码中包含它时,如果不包含 jquery,它就无法工作