如何在Python中反向打印输出?

发布于 2025-01-17 20:25:30 字数 853 浏览 0 评论 0原文

一个问题是要求我将int转换为二进制,但也必须倒转(为什么!??!?!)。经过一堆修补后,我能够将其以二进制打印数字。但是我一生都无法弄清楚如何使其反向输出。

说明说:

编写一个程序,该程序将积极整数作为输入,并输出一个1和0的字符串,代表二进制中的整数。

对于整数X,算法是:

  • 只要x大于0
    • 输出x modulo 2(剩余为0或1)
    • 分配x用x除以2

我的代码是:

x = int(input())

while x > 0:
    x = x//2
    print( x % 2, end = ' ')

输入6的测试,我得到1 1 0,但希望我输出011。

我什至尝试将答案放入一个列表,但是当我尝试扭转列表时,我会发现一个错误。我尝试过的列表方法:

x = int(input())

while x > 0:
    x = x//2
    J = [x % 2]

    L = reversed(J)

    print(L)

使用列表方法输出:

<list_reverseiterator object at 0x7f2cd69484f0>
<list_reverseiterator object at 0x7f2cd6948ee0>
<list_reverseiterator object at 0x7f2cd69484f0>

我觉得这不需要某种切片,因为该方法甚至还没有在我学习的材料中涵盖。

A question is asking me to convert an int into binary, but it also must be in reverse (why!??!?!). After a bunch of tinkering, I was able to get it to print the number in binary. But I can't for the life of me figure out how to make it output in reverse.

The instructions say:

Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.

For an integer x, the algorithm is:

  • 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

My code is:

x = int(input())

while x > 0:
    x = x//2
    print( x % 2, end = ' ')

Testing with input of 6, I get 1 1 0 but it wants me to output 011.

I even tried putting the answer into a list but when I try to reverse the list, I get an error. List method I tried:

x = int(input())

while x > 0:
    x = x//2
    J = [x % 2]

    L = reversed(J)

    print(L)

output using list method:

<list_reverseiterator object at 0x7f2cd69484f0>
<list_reverseiterator object at 0x7f2cd6948ee0>
<list_reverseiterator object at 0x7f2cd69484f0>

I feel like there's no way this needs some sort of slicing since that method hasn't even been covered yet in the material I'm learning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迷爱 2025-01-24 20:25:30

您没有按照给定的顺序遵循提供的算法步骤。 循环中的中的语句交换,以便它们与所描述的内容保持一致。

还有一个小细节:没有指令将输出用空格分开,因此您应该提供end =''

x = int(input())

while x > 0:
    print( x % 2, end = '')
    x = x//2

You didn't follow the provided algorithm steps in the given order. Swap the statements in the while loop so they align with what was described.

And a small detail: there was no instruction to separate the output with spaces, so you should provide end = '':

x = int(input())

while x > 0:
    print( x % 2, end = '')
    x = x//2
聽兲甴掵 2025-01-24 20:25:30

这段代码非常适合我的 zybooks 作业测试。

a: int = int(input())

if a > 0:
    a_str = f'{a:b}'
    print(a_str[::-1])

这也是教科书上的答案:

x: int = int(input())

while x > 0:
   print(x % 2, end='')
   x = x // 2
print()

This code worked perfectly fine for my zybooks assignment test.

a: int = int(input())

if a > 0:
    a_str = f'{a:b}'
    print(a_str[::-1])

Also this is the textbook answer:

x: int = int(input())

while x > 0:
   print(x % 2, end='')
   x = x // 2
print()
一向肩并 2025-01-24 20:25:30

您首先要阅读最不重要的一点,这导致输出逆转。您无需向REVERSED()进行明确调用。

这会产生所需的输出:

x = int(input())

result = []
while x > 0:
    result.append(x % 2)
    x = x // 2

# str() transforms each integer to a string.
# We then concatenate them all together
# to get the desired output using ''.join().
print(''.join(str(item) for item in result))

You're reading in the least significant bit first, which results in the output being reversed. You don't need to make an explicit call to reversed().

This produces the desired output:

x = int(input())

result = []
while x > 0:
    result.append(x % 2)
    x = x // 2

# str() transforms each integer to a string.
# We then concatenate them all together
# to get the desired output using ''.join().
print(''.join(str(item) for item in result))
勿忘初心 2025-01-24 20:25:30
>>> x = 100
>>> res = []
>>> while x > 0:
...   x = x//2
...   J = x%2
...   res.append(J)
...
>>> res
[0, 1, 0, 0, 1, 1, 0]
>>> "".join(str(i) for i in res[::-1])
'0110010'
>>> x = 100
>>> res = []
>>> while x > 0:
...   x = x//2
...   J = x%2
...   res.append(J)
...
>>> res
[0, 1, 0, 0, 1, 1, 0]
>>> "".join(str(i) for i in res[::-1])
'0110010'
思念绕指尖 2025-01-24 20:25:30
step1 = input("what number? ")#gets your input
step2 = int(step1) #makes sure it's an int not float
step3 = bin(step2) #converts it to binairy (you method doesn't work for e.g. 7)
step4 = step3.replace("0b", "") #removes 0b from the binairy number
step5 = step4[::-1] #reveses the string
print (step5)

应该有效
或者

print(bin(int(input("what number? "))).replace("0b", "")[::-1])

如果你想要更压缩的

step1 = input("what number? ")#gets your input
step2 = int(step1) #makes sure it's an int not float
step3 = bin(step2) #converts it to binairy (you method doesn't work for e.g. 7)
step4 = step3.replace("0b", "") #removes 0b from the binairy number
step5 = step4[::-1] #reveses the string
print (step5)

should work
or

print(bin(int(input("what number? "))).replace("0b", "")[::-1])

if you want in more compressed

伪心 2025-01-24 20:25:30

您可以使用Python的特殊函数将整数转换为二进制输入,并在J列表中[:1:-1]的二进制输入反向折叠,因此:

integer_input = int(input())                     # input integert number
binary_of_integer_input = bin(integer_input)     # convert integer to binary
print(binary_of_integer_input[2:])               # Print binary of integer input
print(binary_of_integer_input[:1:-1]) # Print reverse binary of integer input

示例:

Integer编号= 8个

输入的二进制=二进制的1000

反向= 0001

integer_input = int(input())                     # input integert number
binary_of_integer_input = bin(integer_input)     # convert integer to binary
x = binary_of_integer_input[2:]
J = binary_of_integer_input[:1:-1]
print(x)                                         # Print binary of integer input
print(J)                                         # Print reverse binary of integer input

you can use special function of python to convert integer to binary input and the print reverse of binary input by [:1:-1] in J list, so:

integer_input = int(input())                     # input integert number
binary_of_integer_input = bin(integer_input)     # convert integer to binary
print(binary_of_integer_input[2:])               # Print binary of integer input
print(binary_of_integer_input[:1:-1]) # Print reverse binary of integer input

Example:

integer number = 8

binary of input = 1000

reverse of binary = 0001

integer_input = int(input())                     # input integert number
binary_of_integer_input = bin(integer_input)     # convert integer to binary
x = binary_of_integer_input[2:]
J = binary_of_integer_input[:1:-1]
print(x)                                         # Print binary of integer input
print(J)                                         # Print reverse binary of integer input
诗化ㄋ丶相逢 2025-01-24 20:25:30

我正在上这门课!!!!这是包含迄今为止所学材料的代码,该代码有效!对于实际的二进制。除了反转字符串可能没有被提及[::-1]。

实验室希望严格按照该算法得到答案。因此反转二进制并期望它以新行结束。

num = int(input())

while num > 0:
    y =(num % 2)
    print(y, end='')
    num = (num//2)
print()

注意:上述算法以相反的顺序输出 0 和 1。如果解释为“使用此算法转换为二进制但反转它”

num = int(input("Enter a number"))
string = ""

while num > 0:
    y =str(num % 2)
    string+=y
    num = (num//2)

reverse=string[::-1]
print(reverse)

I am taking This class!!!! Here's a code with materials learned so far that works! For actual Binary. Except reversing a string may not have been mentioned [::-1].

The lab wants answers per strictly that algorithm. So reversed binary and expects it to end with new line.

num = int(input())

while num > 0:
    y =(num % 2)
    print(y, end='')
    num = (num//2)
print()

The Note: The above algorithm outputs the 0's and 1's in reverse order. If interpreted "as Convert to binary using this algorithm but reverse it"

num = int(input("Enter a number"))
string = ""

while num > 0:
    y =str(num % 2)
    string+=y
    num = (num//2)

reverse=string[::-1]
print(reverse)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文