如何在Python中反向打印输出?
一个问题是要求我将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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您没有按照给定的顺序遵循提供的算法步骤。 循环中的
中的语句交换,以便它们与所描述的内容保持一致。
还有一个小细节:没有指令将输出用空格分开,因此您应该提供
end =''
: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 = ''
:这段代码非常适合我的 zybooks 作业测试。
这也是教科书上的答案:
This code worked perfectly fine for my zybooks assignment test.
Also this is the textbook answer:
您首先要阅读最不重要的一点,这导致输出逆转。您无需向
REVERSED()
进行明确调用。这会产生所需的输出:
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:
应该有效
或者
如果你想要更压缩的
should work
or
if you want in more compressed
您可以使用Python的特殊函数将整数转换为二进制输入,并在J列表中[:1:-1]的二进制输入反向折叠,因此:
示例:
Integer编号= 8个
输入的二进制=二进制的1000
反向= 0001
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:
Example:
integer number = 8
binary of input = 1000
reverse of binary = 0001
我正在上这门课!!!!这是包含迄今为止所学材料的代码,该代码有效!对于实际的二进制。除了反转字符串可能没有被提及[::-1]。
实验室希望严格按照该算法得到答案。因此反转二进制并期望它以新行结束。
注意:上述算法以相反的顺序输出 0 和 1。如果解释为“使用此算法转换为二进制但反转它”
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.
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"