1
def play(request):
    if request.user.is_authenticated():
        number_of_records = Puzzles.objects.count()
        random_index = int(random.random()*number_of_records)+1
        rand_puzz = Puzzles.objects.get(id = random_index).puzzle
        solution = eval(rand_puzz)
        if 'a' in request.GET:
            a = request.GET['a']
                if a == request.GET['solution']:
                    message = "correct"
                    return render(request, 'play.html',                           {'rand_puzz':rand_puzz,'solution':solution,'message':message})
                else:
                    message = "incorrect"
                    return render(request, 'play.html', {'rand_puzz':rand_puzz,'solution':solution,'message':message})

        else:
            message = 'play the game'
            return render(request, 'play.html', {'rand_puzz':rand_puzz,'solution':solution,'message':message})
    else:
        return render_to_response('home.html')

所以我收到了这个错误:无效的语法(第 1 行)。该页面最初可以工作,然后会发生这种情况。我对这个错误的含义完全感到困惑,任何帮助都将不胜感激

4

1 回答 1

0

找出导致异常的字符串。

 try:
      solution = eval(rand_puzz)
 except SyntaxError:
      print rand_puzz
于 2013-08-14T09:11:38.577 回答