in problem set 2, question 3, i have tried to code as such. it can be run, but didn't print anything. anybody can help me fix it? a=0 b=0 c=0 cannot='' can='' for i in range (1,50): while (6*a+9*b+20*c < i+1): a+=1 if 6*a+9*b+20*c ==i: can=can+'i,' else: if(6*a+9*b+20*c
#continued while(6*a+9*b+20*c <i+1): c+=1 if 6*a+9*b+20*c ==i: can=can+'i,' else: cannot=cannot+'i,' else: cannot=cannot+'i,' print can print cannot
1) put your code into dpaste or similar. It is very easy and no details are required. 2)try inserting print statements at various points to see what is happening. You can remove them at the end. (I do this a lot in debugging).
Sorry for the short anwer I don't have much time right now. But that should get you started...
The things I have spotted: * The big problem if you forget to set a, b, c = 0 at the beginning of your first for loop, so on the second loop round, they are too big to give you a lot of the answers, which is why your can string is empty. * You increment a, b, c at the beginning, so you never have combinations with a,b or c = 0. * Storing your answers in a string seems strange, I'd put it in a list to make it neater.
I found a cute answer which does this is 7 lines, let me know if you're interested in seeing it.
hey all.. thanks for your help... i just realized that i tried to answer the questions before I finished learning about function. for those who didn't realize, there is a timetable here http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/calendar/ to help map the assignments with the module. now i can solve it on 7 line.. lol
Join our real-time social learning platform and learn together with your friends!