Repeating with loops


#1

So, I’m in Launch Code’s Discovery module and we have this problem I can’t quite solve:

“Fill in this code for a program that loops through the numbers between 1 and 40. If a number is divisible by 3, print Hip. If the number is divisible by 7, print Hooray. Remember that there may be numbers that are divisible by both 3 and 7, so you’ll have to print both messages for those.”

This is what I have, but is wrong:


#2

your code has strange indent.

to check for divisibility, we can use the modulo operator.

this would be the design of your program

if divisible by 3 print hip
if divisible by 7 print hooray

if you use if condition for both (so not elif, both messages will be printed for numbers like 21 (divisible by both 3 and 7)


#3

you don’t even have to design the program, piece of cake. Lucky i see this, edits you make don’t give me notifications, and i am in too many topics to revisit them all

as for divisible, you need to use the modulo operator, this gives the remainder, the remainder of a division will equal zero when divisible.


#4

I’ve tried it with the " / " in an IDE and this code works. For some reason this website is not accepting it.


#5

9 is divisible by 3, so Hip should be printed. Lets put that theory to the test

9 / 3 == 0 results in 3 == 0 after resolving division, which is false, so hip won’t be printed, so your code is not working, which is why the exercise is rejecting.

this applies for many numbers in your 1 till 40 range


#6

Here is the solution I came up with (eventually!):


#7

% is named the modulo operator, i think i mentioned the modulo operator twice in my answers

what version of windows are you using? Just curios, it looks old (or its the theme). You don’t have to answer that if you don’t feel like it. Its personal curiosity


#8

“Windows 7 Enterprise” when I’m at work. “Windows 10” at home. Thanks for your help.

–Will.


#9

well, the gray task bar makes it look older. But that is then just the theme :slight_smile:

no problem


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.