p = 0
while ((2**p)*x)%1 != 0:
print(f'Remainder = {str((2**p)*x - int((2**p)*x))}')
p += 1
num = int(x*(2**p))
result = ''
if num == 0:
result = '0'
while num > 0:
result = str(num%2) + result
num = num//2
for i in range(p - len(result)):
result = '0' + result
result = result[0:-p] + '.' + result[-p:]
print(f'The binary representation of the decimal {str(x)} is {str(result)}')```
#๐ need someone who can help me understand the code?
52 messages ยท Page 1 of 1 (latest)
Hey @graceful copper!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
@graceful copper
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.
Closes after a period of inactivity, or when you send !close.
What part specifically are you having difficulties with?
the while loop
and the ending part
Which while loop?
while ((2p)*x)%1 != 0:
print(f'Remainder = {str((2p)*x - int((2**p)*x))}')
p += 1
result = '0' + result
result = result[0:-p] + '.' + result[-p:]
print(f'The binary representation of the decimal {str(x)} is {str(result)}') ```
Hey @graceful copper!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
and this too
What's your understanding of each? The first is just looping and adding to p while the condition is true.
And the for loop just appears to be a verbose way of writing
result = ('0' * (p - len(result))) + result
I'm assuming that's calculating how much padding to add. p would be the total amount of space to take up. So p - len(result) calculates how much shorter result is than the value p.
And then it adds 0 padding in front to make sure that result's length is p.
so they will always be equal ?
!e
total = 10
word = 'hello'
n_padding = total - len(word)
padded = ('0' * n_padding) + word
print(padded)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
00000hello
Count the letters in the output/.
I would print result before and after that line and see what it does.
Inspecting this yourself will be the best way to learn. That's important for debugging anyway.
Do you know what slicing is?
also can we convert all decimal to binary?
Data is stored in binary, so yes.
Well, let me rephrase that. Not all decimal numbers can be stored in binary.
Some decimal numbers, like the result of 1/3 are infinite, so they can't be stored in a finite amount of space.
Resources
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
Tough?
is % 1 intended to check if a float is close to a whole number?
Python and programming are as hard as they are. Good material like good books will make it easier to learn. Bad matieral like YouTube videos that only give the illusion of learning seem easy, but don't actually teach.
which book will you suggest ?
No clue. Read over the Resources link I mentioned. I learned Python more than a decade ago. I can't remember what I used.
You're welcome.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.