0

Hello I am trying to figure out how to go about taking the 3 outputs I have here and reversing the order of them, hopefully without having to remove what I have

My code is:

x = int(input())
while (x > 0):
    remainder = x % 2
    x = x // 2
    if remainder == 0:
      print(0, end=' ')
    else:
      print(1, end=' ')
Esquidit
  • 11
  • 1
  • 1
    I also need to point out `print(format(x,'b'))`. – Tim Roberts Feb 28 '22 at 02:26
  • @TimRoberts where would that go, would it replace bot the if/else print statements – Esquidit Feb 28 '22 at 02:32
  • 1
    It would replace the entire loop. That converts an integer to a binary string. If this is homework, then that would kind of be cheating; you're supposed to get the experience yourself. You need to store the individual digits in a string or list, then use `.reverse` to reverse the list, then print that. – Tim Roberts Feb 28 '22 at 02:33
  • @TimRoberts, It is homework, from what I have without changing the whole thing is there a way to reverse my outputs or would I need to start from scratch you think – Esquidit Feb 28 '22 at 02:39
  • Welcome to Stack Overflow! Please take the [tour]. I closed your question under an existing one about converting an int to a binary string, but I have a hunch there are some constraints you haven't mentioned. In that case, you can [edit] to clarify, but first please read [How to ask and answer homework questions](https://meta.stackoverflow.com/q/334822/4518341) and [ask] in general. In short, you need to try solving the problem yourself first, plus use a more descriptive title. – wjandrea Feb 28 '22 at 02:42
  • @wjandrea, im confused then, That code is my attempt at it and I'm not asking about the answer to the homework overall but just how to reverse the order of my outputs which was explained at the bottom of my question, what did I do wrong – Esquidit Feb 28 '22 at 02:46
  • @Esquidit I'm confused why you're confused. I'm saying, "don't do it like that, use a built-in function that can do it for you". Is there something stopping you from doing that? like, are you not allowed to use a built-in binary converter? – wjandrea Feb 28 '22 at 02:56
  • @wjandrea theres nothing stopping me I was just trying to see if it is possible to use what I have and just reverse it and the end or something. sorry for the confusion or if I sounded rude. – Esquidit Feb 28 '22 at 03:01
  • @Esquidit No problem, you're fine :) As Tim wrote above, it is possible to use what you have, but it's easier to use a built-in solution. Also, to be clear, when I said "you need to try solving the problem yourself first", I meant the problem of reversing the outputs. (Like for example, do you know how to build a list and reverse it?) But that seems moot now. – wjandrea Feb 28 '22 at 03:07
  • I know how to reverse a list I believe but I forget how to build a list from my outputs – Esquidit Feb 28 '22 at 03:10
  • Don't do `print` in each loop. Instead, `append` something to a list (which you need to iniitialize to `[]`). After your loop is through, reverse the list, and print THAT. – Tim Roberts Feb 28 '22 at 04:05

0 Answers0