Приложение Flask получает значение (Нет) из моей формы, прежде чем я ее заполню, что приводит к аномалиям в приложении.Python

Программы на Python
Anonymous
Приложение Flask получает значение (Нет) из моей формы, прежде чем я ее заполню, что приводит к аномалиям в приложении.

Сообщение Anonymous »

Я создаю игру-викторину с помощью Flask и пытаюсь получить информацию от игрока с помощью формы. Во время тестирования я понял, что даже когда я ввожу правильные ответы, оценка не обновляется. Итак, я распечатал ответы игрока на терминал и увидел, что для первого вопроса браузер автоматически присваивает игроку значение NONE, что создает несоответствие между ответом игрока и правильным ответом.
@app.route("/play", methods=['GET', 'POST'])
def play():
score = 0
question = ''
correct_answer = ''
msg = ''
still_playing = True

while still_playing:
# Randomly select a question type
easy_atomic_number = random.randint(1, 31)
easy_element = p_dict[easy_atomic_number]
hard_atomic_number = random.randint(31, 60)
hard_element = p_dict[hard_atomic_number]

if score < 3:
question = f"easy: What is the name of the element with the symbol {easy_element['Symbol']}? "
correct_answer = easy_element['Element']
else:
question = f"Hard: What is the symbol for the element {hard_element['Element']}?"
correct_answer = hard_element['Symbol']

if request.method == 'POST':
user_answer = request.form.get('answer')
if user_answer == correct_answer:
score += 1
msg = 'You are correct'
else:
msg = f'Incorrect, correct answer is {correct_answer}'
print(score)
print(user_answer)
print(correct_answer)
return render_template('play.html', score=score, question=question, correct_answer=correct_answer,
msg=msg)

и вот что я получаю от терминала. Я новичок в этом деле, и мне нужна помощь с проектом, сроки реализации которого быстро приближаются. Пожалуйста, любая помощь будет принята с благодарностью. введите описание изображения здесьСпасибо с нетерпением.
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 117-510-594
127.0.0.1 - - [04/Jul/2024 13:31:22] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [04/Jul/2024 13:31:22] "GET /static/style.css HTTP/1.1" 304 -
127.0.0.1 - - [04/Jul/2024 13:31:22] "GET /static/app.js HTTP/1.1" 304 -
**0
None
Manganese**
127.0.0.1 - - [04/Jul/2024 13:31:25] "POST /play HTTP/1.1" 200 -
127.0.0.1 - - [04/Jul/2024 13:31:25] "GET /static/style.css HTTP/1.1" 304 -
127.0.0.1 - - [04/Jul/2024 13:31:25] "GET /static/app.js HTTP/1.1" 304 -

соответствующий HTML-код


Question:
{{ question }}



Answer

Check


{{msg}} !!!!

SCORE {{ score }}




Quit
Restart



Подробнее здесь: https://stackoverflow.com/questions/787 ... -to-anomal

Вернуться в «Python»