我正在构建一个网络应用程序。我的代码在 return 语句后停止。事后如何保持代码运行?除了烧瓶,我还需要别的东西吗?谢谢!
这是我的 HTML 代码
<!DOCTYPE html>
<html>
<head>
<title>THE QUIZ</title>
</head>
<body>
<h1>QUIZ GAME</h1>
<p>
Welcome
</p>
<h2>Would you like to play</h2>
<form action="/play" method="POST">
Enter your answer: <input type="text" name="answers">
<input type="submit" value="submit">
</form>
</body>
</html>
这是我的python代码
@app.route("/")
def main():
return render_template('app.html')
if request.method == 'POST':
answers = request.form.get('answers')
random.shuffle(questions.qa)
response = quiz(qa, user1, answers)
#this function asks the questions and stores them
return render_template('app.html') + response
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8080, debug=True)
这里是我遇到问题的地方。这是第二个功能。一旦我遍历列表并返回列表中的数据,return 语句下方的代码将无法访问,程序将停止执行它应该执行的操作。如何从列表中返回数据并让代码继续使用 if 语句。我已经搜索过但不明白我发现的结果,因为它们更复杂,而且我是使用 Flask 的新手。
def quiz(qa, user, ans):
for rand_q in qa:
return rand_q['question']
if 'yes' in ans:
user.append('yes')
return rand_q.get(ans, '') #this gets 2nd part of question
我的 if 语句变得无法访问,因此无法从 dict 列表中返回第二组值
这是我修改后的代码,但我仍然坚持在客户输入后返回列表 dict 的第二部分。
def play():
qa = questions.qa #list of dict with questions
user1, user2 = [], [] #containers for each player's answers
if request.method == 'POST':
answers = request.form.get('answers')
random.shuffle(questions.qa)
response = quiz(qa, user1)
return response
def quiz(qa, user):
for rand_q in qa:
i = rand_q['question']
i2 = check_answer(i, user)
#print(rand_q["question"])
return i2
@app.route("/", methods = ['POST', 'GET'])
def check_answer(ans, user):
if request.method == 'POST':
answers = request.form.get('answers')
if 'yes' in answers:
user.append('yes')
i3 = ans.get(answers, '')
return i3 #this should send 2nd part of question/rand_q
答案 0 :(得分:0)
Python 与几乎所有其他编程语言一样,允许您按顺序执行命令。在操作系统内部,python 创建的程序称为进程。命令执行 A 然后 B 然后 C 等等。当然有一些命令可以让你选择命令的流向:如果 A 那么 B 否则 C。但仍然有一系列命令按顺序执行。
现在 return
语句在 python 中做了什么?执行一个函数意味着该函数是从其他一些代码中调用的。然后执行函数的代码,然后执行其他代码中的下一个命令,即在函数调用之后。我们怎么知道这是函数的结尾?这是 return
语句。该函数只能返回一个变量。一旦您已经设置了返回变量,就不需要等待所有其他代码,因为函数的返回不会改变。因此该函数结束并返回您在 return
语句中的内容。
有多种方法可以绕过此过程,具体取决于您想要什么:
1.你想返回多个东西,结果的处理可以等到你计算完所有的结果。在这种情况下,使用 tuple
或 list
从函数返回。例如:
res = []
res.append(res1)
res.append(res2)
return res
2.您希望在结果之一可用时立即返回,并开始与执行后续代码并行处理它。
在这种情况下,您处于并行编程领域。您需要一种在不同并行进程/线程之间进行通信的方法。例如,您可以使用队列:
import queue
q = queue.Queue()
def quiz(qa, user, ans, q):
q.put(res1)
q.put(res2)
def worker(q):
while True:
item = q.get()
print(f'Working on {item}')
# turn-on the worker thread
threading.Thread(target=worker, args=(q,)).start()
在这种情况下,您不需要使用 return 并且一个单独的线程正在处理队列中的项目。
当我更详细地查看您的问题时,您似乎希望在执行第一次返回后有一个 ans
。这是不可能的,因为在函数内部,您无法从外部代码接收任何附加信息,因为外部代码不会被执行(并行编程除外)。你真正需要的是有两个功能。一个选择问题,另一个检查答案:
def quiz(qa, user):
return question
def check_answer(user, ans):
if ans == 'yes':
user.append('yes')
return second_part_of_question
程序的流程应该是:
您的代码很少分享有关您如何在 Flask 中执行此操作的信息,因此我将仅使用带有控制台 Python 应用程序的示例:
question1 = quiz(qa, user)
print(question1)
ans = input()
question2 = check_answer(user, ans)
print(question2)
Flask 中的逻辑是一样的,只是实现方式不同。