#๐ 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
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.
I have a feeling Im headed the right direction, I just cant tell what it is I am missing.
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"
I figured writing it like that to mean "If the number is an integer after dividing number by 3, then print "Fizz".
You will also need to think about your ifs and elifs: do you want to enter the elif if your previous if passed ?
That's not how
I guess not. So they would need to be seperate if statements for each condition im writing
Shhh
Let him think
He read the message already but ๐คทโโ๏ธ๐
Tips: you can use the modulus, you don't have to
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
think about : what property does an integer have ?
its a whole number, and not a float.
True, what a property a float has that an int do not ?
There are several good answers
decimal points?
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
Not necessarily
Alright
If N is an integer (or a float ending by .0)
What's int(N) ?
its whatever N is equal to
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
are you talking about making it a string?
You don't have to
Like you can, but is logically not a good solution imo
Stick with number
And what I asked you
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.
Im trying to tell the program that if (number from range is divisible by 3, and is an integer) print as "Fizz instead"
converting a float to an integer with int() will just remove/truncate any decimals, not even round it, it's effectively a floor() operation
That's not what you are doing
oh so it might even just divide every number and remove the decimal then, which i dont want either
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
that should only be true for 0
You want the integer part of the third of the number to be equal to the third of the number
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.
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?
maybe spell out everything with variable names first to sort it out
third_of_number = ...
int_of_third_of_number = ...
..etc
Normally this would help. But as I said, I think im just having trouble wrapping my brain around it.
You are there
so close, line 4
I gotta go andrew I let you finish helping him, also about the assignation and print statement
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 ?
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
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
Try describing line by line what your current algorithm is doing
And spot the differences with what it should do
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
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
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.
Tunnel vision is common, and stepping away is usually a good idea. Let us know here if you get it. ๐
just know that these help threads are automatically closed by a bot after an hour without any activity, so if you or anyone else isn't keeping it alive it will get closed, and you'll have to open a new help thread
another tip, the // operator also divides, but it always gives you an integer by flooring the result
you will end up with the same result as putting int() around your division or the result of your division
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
i feel like this is a good time to tell them about the modulo operator
and floor division just for funsies
he said he ain't that far yet into the course he's learning
You can just do this
for number in range(1, 101):
if number % 3 == 0:
print(f'{number} frizz')
the modulo operator give the remaining of a division and == check if said remaining is equal to 0.
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.