#๐Ÿ”’ need someone who can help me understand the code?

52 messages ยท Page 1 of 1 (latest)

graceful copper
#

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)}')```
south portalBOT
#

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.
south portalBOT
#

@graceful copper

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.

worthy cave
graceful copper
#

and the ending part

worthy cave
graceful copper
#

while ((2p)*x)%1 != 0:
print(f'Remainder = {str((2
p)*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)}') ```
south portalBOT
#

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
#

and this too

worthy cave
#

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
graceful copper
#

(p - len(result))

#

y this

worthy cave
#

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.

graceful copper
#

so they will always be equal ?

worthy cave
#

!e

total = 10
word = 'hello'
n_padding = total - len(word)

padded = ('0' * n_padding) + word
print(padded)
south portalBOT
worthy cave
#

Count the letters in the output/.

graceful copper
#

ohk i get it

#

result = result[0:-p] + '.' + result[-p:]

#

now what about this?

worthy cave
#

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?

graceful copper
#

also can we convert all decimal to binary?

graceful copper
#

basic

worthy cave
#

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.

graceful copper
#

ohk

#

Btw

#

i was doing python from a book

#

should i do it from yt?

worthy cave
#

I've seen a lot of trash on YouTube. I have more faith in books.

#

!resources

south portalBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

graceful copper
#

i think book is a bit tough no?

#

for me?

worthy cave
#

Tough?

stuck cradle
#

is % 1 intended to check if a float is close to a whole number?

worthy cave
#

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.

graceful copper
worthy cave
graceful copper
#

oh alr

#

thank you

worthy cave
#

You're welcome.

south portalBOT
#
Python help channel closed

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.