Can any one help with this code a=12 while a<50: if (a/6*6==a): print ('6 is a factor') else: a=a+6 print ('6 is not a factor') @MIT 6.00 Intro Co…
What didn't you understand in the code? I will try to explain the general ideas: Firstly, the while loop will repeat the indented code while a < 50 (quite like in English). The if-else statement will check for boolean values, i.e. 'true' or 'false'. The if check works abusing of one thing: integer division in Python. Integer division in Python (2.x) returns the floor, not a floating point number, hence if a%6 != 0 (aka 'a' is not divisible by 6), then a/6 will be the floor, so (a/6)*6 won't return the value of 'a' to what it was before. If a%6 == 0, then (a/6) will be a precise integer value, and (a/6)*6 will bring the value of 'a' back to what it was before.
oh i wrote the code but when it runs and you give it a number that 6 goes into then it just repeats "6 is a factor". I know that it is because its still in the loop but i cant figure out how to get it out of the loop
You should go out from loop if statement "(a/6*6==a)" is true. For example, if (a/6*6==a): print ('6 is a factor') break else: a=a+6 But in this case, for a = 12 program output "6 is a factor 6 is not a factor", because it always output "6 is not a factor" in any case. It isn't correct. p.s. sorry for bad english.
Two things first: could you paste your code on pastebin or codepad? It makes it easier to see. Secondly: what precisely do you want the code to do? Write comments in your code to make it easier for us to understand it.
I have been stuck on problem set 2 two for a few days and that was my round about way of thinking how to start the problem off http://pastebin.com/GnTsjjhX
How about using the '%' operator for it? Returning the modulus, then you can check for divisibility easier. And why are you incrementing a by 6, 9 and 20? I think I understand what you are trying to do (trying to generate combinations of multiples of 6, 9, and 20?), but shouldn't you rather muscle it out first? Isn't it easier to exhaust all the possibilities first? This kind of algorithm (brute force) has a bad time complexity, but here we are working with relatively small numbers and your computer likely has more than 1GHz of processing power. Then you can think about improving the code. As a tip: try searching for nested for loops in python, or something like that, on google, and check the readings tab at the home page of the course.
Ya i spaced the reading tap, I found it yesterday so i have a lot of reading ahead of me thanks for the help.
One problem with your approach is that it will only detect if a number is buyable using only a single package size. For example, it won't find 46 because you have to use 6- and 20-piece packages. But you need to be able to find those numbers as well.
BMP, can you define the word 'floor' in this statement? "Integer division in Python (2.x) returns the floor, not a floating point number" I've never heard the term before and I'm curious. Thanks mate
The smallest integer of the division, like in the math.floor() function from the math module. 11/5 returns 2, instead of 2.2, for instance, while 11.0/5, 11.0/5.0, 11/5.0, or 11/5 with from __future__ import division returns 2.2. It's a feature from Python that came from C, but they removed it in 3.x, in face of its ambiguity. Now, the "true" int division operator is "//". Check PEP 238: http://www.python.org/dev/peps/pep-0238/
Join our real-time social learning platform and learn together with your friends!