Pythonanywhere - 如何显示我的代码?

时间:2017-05-29 09:43:42

标签: python flask pythonanywhere

我必须创建一个Web应用程序,使用pythonanywhere和flask为您提供基于您必须回答的问题的建议。我该怎么做? 例如,我有来自其他人的代码:

        from random import shuffle

    question_list = [  # tuple of the form (question, dict of answers)
        ('What is the population of New Zealand ?',
        {'6.7': False, '3.2': False, '5.1': False, '4.5': True}),

        ('What year did the first european set foot on New Zealand Abel Tasman ?',
        {'1830': False, '1543': False, '1765': False, '1642': True}),

        ('How many Kiwi are there left in New Zealand Approx ?',
        {'2000': False, '600': False,  '70,000': False, '100000': False, '100000': True}),

        ('How many new babys where born in New Zealand in 2015 ?',
        {'61,000': False, '208,000': False, '98,000': False, '18,000': True}),
    ]


    def get_input_in_list(lst):
        while True:
            print("Please enter value from : " + " ".join(lst))
            user_input = raw_input()
            if user_input in lst:
                return user_input


    def questions():
        wrong = 0
        right = 0

        for each_question, answer_dict in question_list:
            answers = list(answer_dict)
            shuffle(answers)
            print(each_question)
            user_answer = get_input_in_list(answers) 

            if answer_dict[user_answer]:
                print('Your answer is correct!\n')
                right += 1
            else:
                print('That is not the answer I had in mind!\n')
                wrong += 1
            print('So far, you answered correctly to {0} questions and incorrectly to {1}. Good luck!'.format(right, wrong))


    if __name__ == '__main__':
        questions()

我将其粘贴在 / home /(用户名)/mysite/flask_app.py 中,但我如何在我的网站上显示?我知道我必须在 / home /(用户名)/mysite/templates/main_page.html 中使用html做一些事情,但我不知道是什么。

希望有人可以提供帮助,

谢谢:)

1 个答案:

答案 0 :(得分:0)

我从上面的代码改变了一些东西。在我的回答中添加了很多东西。

from flask import Flask, render_template, request

app = Flask(__name__)

question_list = [  # tuple of the form (question, dict of answers)
    [
        'What is the population of New Zealand ?',
        {'6.7': False, '3.2': False, '5.1': False, '4.5': True},
        {'is_correct': False, 'answer': '4.5'}
    ],

    [
        'What year did the first european set foot on New Zealand Abel Tasman ?',
        {'1830': False, '1543': False, '1765': False, '1642': True},
        {'is_correct': False, 'answer': '1642'}
    ]
]

@app.route('/', methods=['GET', 'POST'])
def questions():
    if request.method == 'POST':

        post_question_list = question_list
        correct_answers = 0
        for key, answer in enumerate(request.form.getlist('answer')):
            is_answer_correct = post_question_list[key][1][answer]

            if is_answer_correct:
                correct_answers += 1

            # Update with correct answer
            post_question_list[key][2]['is_correct']  = is_answer_correct

        return render_template('index.html', question_list=post_question_list, correct_answers=correct_answers)

    return render_template('index.html', question_list=question_list)


if __name__ == '__main__':
    app.run()

在你的HTML中

<html>
  <head>
  </head>
  <body>
    <form action="." method="post">
      {% for question in question_list %}
        <h3>{{ question.0 }} </h3>
        <small>{% if question.2.is_correct %}Your answer is correct {% endif %}</small>
        <select type="checkbox" name="answer">
          {% for choice in question.1 %}
            {% if question.2.is_correct %}
              <option value="{{ choice }}">{{ question.2.answer }} </option>
            {% else %}
              <option value="{{ choice }}">{{ choice }} </option>
            {% endif %}
          {% endfor %}
        </select>
      {% endfor %}
      <input type="submit" value="submit">
    </form>

    {% if correct_answers %}
      You answered correctly to {{ correct_answers }}
    {% endif %}
  </body>
</html>