#๐Ÿ”’ Converting numbers into reverse binary???

60 messages ยท Page 1 of 1 (latest)

errant glen
#

I don't really know what that means but the instructions are
As long as x is greater than 0
Output x modulo 2 (remainder is either 0 or 1)
Assign x with x divided by 2

I've done that with my code but it wont stop looping

int_value = int(input())

while int_value >= 0:
q = int_value % 2
if q == 0:
print(0)
else:
print(1)
int_value = int_value // 2

Also how do you make it so that 2 diffrent outputs are on the same line? I may have totally forgot...

thorny stormBOT
#

@errant glen

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.

errant glen
#

here is the error

#

It works fine for about 3 lines then just prints 0s

#

while int_value >= 0:    
    q = int_value % 2
    if q == 0:
        print(0)
    else:
        print(1)
    int_value = int_value // 2```
thorny stormBOT
#

Hey @errant glen!

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.
shy needle
#

your while loop says While int_value is greater or equal to 0 do this

errant glen
shy needle
#

so, int_value has to be negative in order to leave the while loop

#

so how would it be possible for int_value to become negative?

errant glen
#

I actually have no clue i just did what i was told and hoped it would work ._.

#

i dont really get loops yet tbh

shy needle
#

think it from a mathematical perspective, is it possible to divide a positive integer, by another positive integer and get a negative one?

errant glen
#

no

#

yeah ok that makes sense

shy needle
#

so your while loop is always true

#

when do you really want your loop to end?

errant glen
#

i guess when it reaches 1 because it cant be divided past that

#

yay it works now! thanks :D

shy needle
#

hehe no problem

errant glen
#

also how do i print multiple things on the same line

shy needle
#

depends

#

do you have variables?

#

to print on the same line?

#

or what exactly do you want? to print it on the same line using a loop?

errant glen
#

just numbers 1 and 0

shy needle
#

!d print

thorny stormBOT
#

print(*objects, sep=' ', end='\n', file=None, flush=False)```
Print *objects* to the text stream *file*, separated by *sep* and followed by *end*. *sep*, *end*, *file*, and *flush*, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like [`str()`](https://docs.python.org/3/library/stdtypes.html#str) does and written to the stream, separated by *sep* and followed by *end*. Both *sep* and *end* must be strings; they can also be `None`, which means to use the default values. If no *objects* are given, [`print()`](https://docs.python.org/3/library/functions.html#print) will just write *end*.

The *file* argument must be an object with a `write(string)` method; if it is not present or `None`, [`sys.stdout`](https://docs.python.org/3/library/sys.html#sys.stdout) will be used. Since printed arguments are converted to text strings, [`print()`](https://docs.python.org/3/library/functions.html#print) cannot be used with binary mode file objects. For these, use `file.write(...)` instead.
shy needle
#

take a look at the end parameter

#

!e

print("aaaaaaaaa", end="")
print("Bbbbbbbbbb")
thorny stormBOT
#

@shy needle :white_check_mark: Your 3.12 eval job has completed with return code 0.

aaaaaaaaaBbbbbbbbbb
errant glen
#

ohhhhh

shy needle
#

you see, by the default, the value of the end parameter is \n which means new line

#

so everytime you print something, it ends with \n (for computers) so they know to go and print the next thing on the next line

#

but you can play with this and make the end parameter a empty string "" therefore, printing everything next to eachother

errant glen
#

oh ok! thank you :D

shy needle
#

no problem

#

also, if you want to make it a bit nicer, you can get rid off the q variable

#
int_value = int(input())

while int_value > 0:    
    if int_value % 2 == 0:
        print(0)
    else:
        print(1)
    int_value = int_value // 2
#

and you can check directly, without needing to store the data in another variable

errant glen
#

Oh. I knew i overcomplicated something

shy needle
#

ah no worries, there are more complicated or simpler things

#

like

#
int_value = int(input())

while int_value > 0:    
    print(1 if int_value % 2 == 0 else 0)
    int_value =// 2
#

it should work the same

#

but some could find it simpler, because there are less lines, but others complicated, if they do not know the in-line if-else

errant glen
#

you know this was was simpler than i thought

#

aparently simpler than spelling simpler

shy needle
#

!e

int_value = 146
while int_value > 0:    
    print(0 if int_value % 2 == 0 else 1,end="")
    int_value //= 2
thorny stormBOT
#

@shy needle :white_check_mark: Your 3.12 eval job has completed with return code 0.

01001001
shy needle
#

if you practice enough you can do unimaginable things

#

also if you want to see one-line codes that do a lot of things you can go to #esoteric-python

errant glen
#

ooooh oki :D thanks!

shy needle
shy needle
errant glen
#

ok!

#

!solved

thorny stormBOT
#
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.