as mentioned in title, want display 0 instead of none session.counter or session.wrong_counter in web2py app. have tried 10 different ways not getting right reason.
this controller page score:
def score(): image = db.pic(request.args(0,cast=int)) or redirect(url('index')) correct = image.id == session.pic_id next_game = random.randint(1, 35) wrong_counter = [] right_counter = [] if correct: session.counter = (session.counter or 0)+1 right_counter=session.counter if not correct: session.wrong_counter = (session.wrong_counter or 0)+1 wrong_counter=session.wrong_counter return locals() in view have {{=session.counter}} , {{=session.wrong_counter}}. when page score first visited during session 1 of them display 1 , other display none depending on correct or not correct conditions expected, however, want display 0 instead of none.
i appreciate advise.
simply initialize wrong_counter value 0 when correct true, , wrong_counter not set:
if correct: session.counter = (session.counter or 0)+1 right_counter=session.counter if not session.wrong_counter: # first time have correct answer wrong_counter = 0 else: wrong_counter = (session.wrong_counter or 0)+1 session.wrong_counter = wrong_counter if not correct: session.wrong_counter = (session.wrong_counter or 0)+1 wrong_counter=session.wrong_counter that way, instead of having none default value, have 0.
however, have set right_counter , wrong_counter empty lists above loop. if set these value of session variables, make logic easier:
right_counter = session.counter or 0 wrong_counter = session.wrong_counter or 0 if correct: right_counter = int(right_counter) + 1 if not correct: wrong_counter = int(wrong_counter) + 1 session.counter = right_counter session.wrong_counter = wrong_counter
Comments
Post a Comment