#๐Ÿ”’ Doing a coding exercise using for loops, and range function. Why doesn't this print what I expect?

99 messages ยท Page 1 of 1 (latest)

candid tinsel
#

I want to print a range of numbers from 1 to 100.
But every number that is a factor of 3, I want to print "Fizz", every number that is a factor of 5, I want to print "Buzz", and every number that is a factor of 15, I want to print "FizzBuzz".

frozen fjordBOT
#

@candid tinsel

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

candid tinsel
#

I have a feeling Im headed the right direction, I just cant tell what it is I am missing.

shy kraken
#

You are comparing the right thing

#

Now you are doing :
Is number equal to its third ? Then print "Fizz"

#

etc.

#

You want to do:
Is the third of the number an integer ? Then print "Fizz"

candid tinsel
#

I figured writing it like that to mean "If the number is an integer after dividing number by 3, then print "Fizz".

shy kraken
#

You will also need to think about your ifs and elifs: do you want to enter the elif if your previous if passed ?

candid tinsel
#

I guess not. So they would need to be seperate if statements for each condition im writing

shy kraken
#

Shhh

#

Let him think

#

He read the message already but ๐Ÿคทโ€โ™‚๏ธ๐Ÿ˜…

#

Tips: you can use the modulus, you don't have to

candid tinsel
#

Yeah i haven't made it that far in the course im taking lol

#

okay i know how to fix my if statements, but im blanking on how to write this equation differently

#

the third of every number an integer.

#

well i cant add steps because I want all the other numbers included in the list

shy kraken
#

think about : what property does an integer have ?

candid tinsel
#

its a whole number, and not a float.

shy kraken
#

True, what a property a float has that an int do not ?

#

There are several good answers

candid tinsel
#

decimal points?

shy kraken
#

That is one good answer

#

If you already know about strings, you can manage to do it

#

Wait no

#

Python doesn't work like that

#

Integers have a decimal point

#

Only it is hidden

#

Like 3 is equal to 3.0

#

And python divison return a float

#

So you need another property

candid tinsel
#

well thats good to know

#

idk man integers lack accuracy

shy kraken
#

Not necessarily

#

Alright

#

If N is an integer (or a float ending by .0)

#

What's int(N) ?

candid tinsel
#

its whatever N is equal to

shy kraken
#

And if N isn't an integer, what is int(N)

#

Or better, what isn't ?

floral tartan
#

3 is only represented as 3.0 if it's a float, not when it is an int
and it's correct that division always returns a float in python

candid tinsel
#

are you talking about making it a string?

shy kraken
#

You don't have to

#

Like you can, but is logically not a good solution imo

#

Stick with number

#

And what I asked you

candid tinsel
#

I can't think of any other properties. If its going to be a float after I divide it, then it doesn't matter, unless I convert the result to a int, which is what i thought i was doing.

shy kraken
#

It is indeed what you are doing

#

But what are you currently comparing int(N/3) with ?

candid tinsel
#

Im trying to tell the program that if (number from range is divisible by 3, and is an integer) print as "Fizz instead"

floral tartan
candid tinsel
#

oh so it might even just divide every number and remove the decimal then, which i dont want either

shy kraken
#

Read carefully your code

#

You are currently printing Fizz is a number is equal to int(number/3)

#

So if a number is equal to the integer part of its third

floral tartan
#

that should only be true for 0

shy kraken
#

You want the integer part of the third of the number to be equal to the third of the number

candid tinsel
#

Yeah I keep rewriting it, only for it to end up the exact same. So I guess its a conceptual issue on my part. Let me go back and read through everything here again.

shy kraken
#

You want the integer part of the third of the number to be equal to the third of the number

#

Let's get it step by step:
Let N an integer
What is "the integer part of the third of N" ?

#

Then, what is the "third of N"

#

Then, how can you compare if the two are equal ?

#

And finally:
What do you do in that case?

ancient quiver
#

maybe spell out everything with variable names first to sort it out

third_of_number = ...
int_of_third_of_number = ...
..etc
candid tinsel
shy kraken
ancient quiver
#

so close, line 4

shy kraken
#

I gotta go andrew I let you finish helping him, also about the assignation and print statement

ancient quiver
#

as @shy kraken said:

You want the integer part of the third of the number to be equal to the third of the number

Is that what you have on line 4?

#

And I'll give you this one for free: your indentation is wrong on line 8

#

print is outside of the for loop, which you don't want

#

How 'ya doing @candid tinsel ?

candid tinsel
#

well thank you for that freebie, it did change the result

#

instead of printing 100 like normal. It just lists the range normally from 1 to 100

ancient quiver
#

good. you can probably limit that to 20 for now, so the output isn't so long

#

but there's still a mistake on line 4

#

and you're probably overthinking it.

I can't think of any other hint than:

You want the third of the number to be equal to the integer part of the third of the number

Try reading it out loud

shy kraken
#

Try describing line by line what your current algorithm is doing

#

And spot the differences with what it should do

candid tinsel
#

so like this?

#

int_of_third_number = int(third_of_number / 3)

#

int of the third number equal to the third number of the integer

ancient quiver
#

no, at that point you'd be dividing by 9... because you're taking 1/3rd of 1/3rd of the number

#

how about changing your print call to
print(number, int_of_third_number, third_of_number)

#

maybe seeing the data would help achieve your goal

candid tinsel
#

I've been doing this course work for a couple hours and Im just getting tunnel vision I think. Im gonna step away for a couple of hours, and revisit everything here and see what I can come up with then. Thank you guys. Sorry for the headache. This course moves pretty quickly and I don't want to get in the habit of getting stuck, looking at the solution and learning nothing.

ancient quiver
#

Tunnel vision is common, and stepping away is usually a good idea. Let us know here if you get it. ๐Ÿ‘‹

floral tartan
floral tartan
ancient quiver
#

I had another option to suggest:
float objects have a method f.is_integer() which simplifies the logic:

>>> f = 1 / 3
>>> f.is_integer()
False
>>> f = 3 / 3
>>> f.is_integer()
True
rocky nacelle
#

i feel like this is a good time to tell them about the modulo operator

coarse garnet
#

and floor division just for funsies

timid needle
timid needle
frozen fjordBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.