python - Trying to figure out syntax to evaluate a string from the database using Django -


i have question regarding method eval, wanted create separate def handle user inputs answers form.. problem method wont work because don't know how pass local variable def since random value different in function anyway.

essentially ran_puzz pulls random puzzle database has value "2+2" etc.

rand_puzz = puzzles.objects.get(id = random_index).puzzle

so figured since did not know how pass rand_puzz def, decided keep variable , logic inside def called "play" , return more 1 value like" message , ran_puzz"

if see line "if eval(rand_puzz)== user_sub_ans;" // causes syntax errors

urls:

url(r'^play/$', 'mathgame3.views.play'), 

views: 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     user_sub_ans = request.get['answer']         if eval(rand_puzz)== user_sub_ans;     message = 'correct'     else:     message = 'incorrect'     return render(request, 'play.html', {'rand_puzz': rand_puzz, 'message': message}) else:     return render_to_response('home.html') 

html: chunk of code form user inputs answer puzzle.

    <form action="/play/" method = "get">         <table style="margin-left:auto; margin-right:auto; width:auto;"> <table style="margin-left:auto; margin-right:auto; width:auto; border:solid 1px"> <tr><td><label for="username">question:</label></td> <td>{{rand_puzz}}</td></tr> <tr><td><label for="answer">answer:</label></td> <td><input type="number" name="answer" value="" id="answer"></td></tr> <td> answer is: {{message}}</td></tr> <tr><td></td><td><input type="submit" value="submit"/></td></tr> </table>         </table>     </form> 

i appreciate help.

that should like:

if eval(rand_puzz)== user_sub_ans:      message = 'correct' else:      message = 'incorrect'  

the puzzle field not value user input hope.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -