Need help on PS2 problem 3. Having coding issues I cannot seem to fix. http://codepad.org/6fCxOfvq It is listing values like 55 and 40 which should have passed the test in line 17 and not the test in line 19. Working it out by hand: 40/6 +1 = 6+1 = 7 x = 40. So a = range(0,7) or 0,1,2,3,4,5,6. b = range(0,5) and c = range(0,3) 40 can factor to a=0, b=0, c=2 6*a[i] + 9*b[j] + 20*c[k] == x a[0] = 0 b[0] = 0 c[2] = 2 6*0 + 9*0 + 20*2 == x == 40 Thus, the number 40 should pass that test, fail the second, and NOT be printed. But it does get printed. Same with 55, 52, 195 and others.
I added a few print commands in your code, they should help you figure out why your program never actually tests if "6*0 + 9*0 + 20*2 == x == 40". Also i suggest that you edit your loop condition, as your program continues beyond what is necessary. http://codepad.org/5QReVMGh
I notice that the i, j and k values did not reset if a value can be reached (bought) and only reset when they cycle completely. This means that the loop is not functioning the way I had planned. I was under the assumption that when X was changed, the lowest level nested loop involving X would reset. That would be the "while (x<=variable)" line. However, it would seem that it does not always reset to this point, and continues in its current state until it either hits the ceiling of i-j-k or hits a value it deems unreachable. In the case of 40, the k term is never listed as 2 in its set. After stuffing in some reset values, I managed to make sure it reset after each conclusion. However, it still would not reset to the "while (x<=variable)" line which meant it would continue to infinity. When I had it print for me x/20, it gave the correct response (2) but c[-1] came back as 1 and c itself came back as [0, 1]. When I tried printing c[x/20] it gave me an index-out-of-range error. I honestly have no idea how the sequencing of nesting got messed up, nor how x/20 can be correct but not c[x/20]. There is nothing I can determine that would throw off the computation, and I have spent the majority of the day trying to figure out what id happening. I do not know any goto commands, since I have been working directly off the lectures, but I imagine a goto loop might work a little better. I am sorry for the lengthy response, but my inferior debugging skills cannot find the cause, only the symptoms.
Did you make sure you are using floating points and integers where necessary?
I suggest you look at the break statement, which when reached in a loop exits it, and maybe the "for loop" although the break statement works with the while loop as well. http://en.wikibooks.org/wiki/Python_Programming/Flow_control @LocalNomad: Floating points will ruin his range definitions as the range function needs an integer as end argument.
Join our real-time social learning platform and learn together with your friends!