Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help3403


This forum is provided to promote discussion amongst students enrolled in Agile Web Development (CITS3403).
 
Options:
RSS cloud
Jump to:

Why does changing one input break this code segment?

5 of 419 articles shown, currently no other people reading this forum.
photo
From: ANONYMOUS
Date: Fri 22nd May, 3:34am
Actions: 
        Login-to-reply
I'm trying to use a link to the quiz page and automatically display the chosen quiz by 
using types and then "int(request.args.get('type'))"  however when I then go to use this 
type to filter the quizes by quizCode, this breaks my code and gives me an index out of 
bounds error. I tried converting the type to an int but it seems to somehow turn itself 
back into a NoneType every-time the code tries to run and I'm not sure why. The code works 
fine if I manually type quizCode='1' for example but when using selectedQuiz it breaks.

Why does changing one input break this code segment?

photo
From: Haolin W.
Date: Fri 22nd May, 10:06am
Actions: 
        Login-to-reply
Pretty sure the quiz= part needs .first() on the end or .all() depending on what you want to do. 
Whatever comes from the form will always be a string, so casting it into an integer is correct. 
It's throwing index out of bounds likely because you have literally nothing for quiz[i].answer in 
your loop, as the quiz= line will always return the SQL statement rather than an actual object 
list.

In the future, put print statements before things you're trying to debug, to make sure that 
you're getting exactly what you're expecting at each stage of the POST. I haven't actually run 
your code, but an index out of bounds error means it's something to do with your loop, and that's 
the most likely place for it to happen.

ANONYMOUS wrote:

> I'm trying to use a link to the quiz page and automatically display the chosen quiz by 
> using types and then "int(request.args.get('type'))"  however when I then go to use this 
> type to filter the quizes by quizCode, this breaks my code and gives me an index out of 
> bounds error. I tried converting the type to an int but it seems to somehow turn itself 
> back into a NoneType every-time the code tries to run and I'm not sure why. The code works 
> fine if I manually type quizCode='1' for example but when using selectedQuiz it breaks.

Why does changing one input break this code segment?

photo
From: ANONYMOUS  O.P.
Date: Fri 22nd May, 10:15am
Actions: 
        Login-to-reply
I've had all the prints there in the past to check and before the looping starts everything was working. 
The code functions fine when I manually input '1', '2', '3' etc in the filter by. It just breaks when I 
input selectedQuiz, which when printed is = to the exact same thing which is why I'm confused.

Why does changing one input break this code segment?

photo
From: Haolin W.
Date: Fri 22nd May, 10:19am
Actions: 
        Login-to-reply
Have you tried changing the filter_by part to filter_by(quizCode=1).all() ? Changing the string to an integer, 
as well as adding the all function? From my experience leaving out the all part results in the raw SQL query 
instead of the actual list. What does printing out selectedQuiz return? What does printing quiz return?

I can join the lab zoom or haolin#2060 if you want to screenshare.

Why does changing one input break this code segment?

photo
From: Takoyaki N.
Date: Fri 22nd May, 11:22am
Actions: 
        Login-to-reply
Apparently if you don't specify .all() or .first() it defaults to .all() but I would still specify 
.all() to make your code explicit.

When you are getting the index out of bounds error what index is it throwing the error at?  Perhaps 
add a print(i) just inside the loop.  If you are passing X mycheckboxs, the query is returning Y 
results then you will get an index error if X > Y

Perhaps add this code to see if it makes things clear for you.

print(Quiz.query.filter_by(quizCode=request.args.get('type').all().count())
print(range(len(answered)))

 





ANONYMOUS wrote:

> I'm trying to use a link to the quiz page and automatically display the chosen quiz by 
> using types and then "int(request.args.get('type'))"  however when I then go to use this 
> type to filter the quizes by quizCode, this breaks my code and gives me an index out of 
> bounds error. I tried converting the type to an int but it seems to somehow turn itself 
> back into a NoneType every-time the code tries to run and I'm not sure why. The code works 
> fine if I manually type quizCode='1' for example but when using selectedQuiz it breaks.
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020