地理测验python

时间:2017-06-27 14:26:21

标签: python-2.7

我的代码存在问题。我对python很新,我尝试新事物。我想创建一个地理测验,随机("但不是重复")选择我必须猜测其资本的国家。目前,对于每个答案,我都会收到一条消息,说明我的答案是错问题是我应该怎么做才能当我在一个国家的字典中循环时,我将不得不猜测该国家的正确密钥。

以下是代码:

#!/usr/bin/env python
import os
import jinja2
import webapp2
import random


template_dir = os.path.join(os.path.dirname(__file__), "templates")
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir), autoescape=False)

class BaseHandler(webapp2.RequestHandler):

    def write(self, *a, **kw):
        return self.response.out.write(*a, **kw)

    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)

    def render(self, template, **kw):
        return self.write(self.render_str(template, **kw))

    def render_template(self, view_filename, params=None):
        if not params:
            params = {}
        template = jinja_env.get_template(view_filename)
        return self.response.out.write(template.render(params))

class MainHandler(BaseHandler):
    def get(self):
        return self.render_template("geokviz.html")
class AboutHandler(BaseHandler):
    def get(self):
        return self.render_template("geokviz1.html")

class flagHandler(BaseHandler):
        def get(self):
            return self.render_template("flag.html")
class attractionHandler(BaseHandler):
    def get(self):
        return self.render_template("attraction.html")

class Capital:
    def __init__(self, capital, country):
        self.capital = capital
        self.country = country

city = {"Zagreb":"Croatia", "Ljubljana":"Slovenia", "Rome":"Italy", "Oslo":"Norway"}


class capitalCityHandler(BaseHandler):
    def get(self):
        for cities in random.sample(list(city), 1):
            capital = city[cities]

            params={"capital": capital}
            return self.render_template("capital.html", params=params)
    def post(self):
            odgovor = self.request.get("answer")
            if odgovor == city.keys():
                result1 = "You are right"
            else:
                result1 = "You are wrong"
            self.write(result1)

app = webapp2.WSGIApplication([
    webapp2.Route('/', MainHandler),
    webapp2.Route('/geo', AboutHandler),
    webapp2.Route('/city', capitalCityHandler),
    webapp2.Route('/flag', flagHandler),
    webapp2.Route('/attraction', attractionHandler),


], debug=True)

感谢所有帮助

1 个答案:

答案 0 :(得分:0)

首先,您需要检查资本是否存在作为关键字,然后您可以检查它是否与国家匹配。

.done