from flask import Flask, request, render_template
app = Flask(__name__)
def fun(userAns="True", question="this is first question"):
if question == "this is first question":
if userAns == "True":
return "actual first question", "Yes", "No", None
elif question == "actual first question":
if userAns == "True":
return "2nd question to be asked after ans = true", "Yes", "No", None
elif userAns=="False":
return "2nd question to be asked after ans = false", "Yes", "No", None
question = None
userAns = None
@app.route("/", methods=["POST", "GET"])
def hello():
a=0
while a < 1:
question = "this is first question"
userAns = 'True'
returnValue = fun(userAns, question)
a += 1
if request.method == "POST":
userAns = request.form["answer"]
print(userAns)
returnValue = fun(userAns, question)
question=returnValue[0]
return render_template("index.html",question=returnValue[0], option1= returnValue[1],option2=returnValue[2],option3=returnValue[3])
if __name__ == "__main__":
app.run(debug=True)
Here I want to call the fun()
function in the @app.route
, but for that I have to initialize some variables as they will be updated for the next iteration. I observed in the debugger that every variable gets its initial value at the end of @app.route
, so I tried to make those variables global, but it didn't take global variables for some reason.
To update a global variable in function use this global variable_name
at the start of hello function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.