【问题标题】:Pythonanywhere - how to display my code?Pythonanywhere - 如何显示我的代码?
【发布时间】:2017-05-29 09:43:41
【问题描述】:

我必须使用 pythonanywhere 和 flask 制作一个 Web 应用程序,它会根据您必须回答的问题为您提供建议。我该怎么做? 例如,我有其他人的代码:

        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/(username)/mysite/flask_app.py 中,但如何在我的网站上显示它?我知道我必须对 /home/(username)/mysite/templates/main_page.html 中的 html 做一些事情,但我不知道是什么。

希望有人能帮忙,

谢谢:)

【问题讨论】:

    标签: python flask pythonanywhere


    【解决方案1】:

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

    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>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多