I have an assignment for my Python course that I'm struggling with.
我有一项Python课程的作业,我正在努力完成。
We are supposed to make a function that prints out binary as follows:
我们应该制作一个打印二进制文件的函数,如下所示:
If the input is:
如果输入为:
chessboard(3)
It should print out:
它应该打印出来:
101
010
101
And so forth..
等等
Its a "simple" program but I'm really new to coding.
这是一个“简单”的程序,但我对编码真的很陌生。
I can produce a while loop that writes out the correct length and amount of lines but I'm struggling to produce variation between the lines.
我可以生成一个while循环,写出正确的行长度和数量,但我很难在行之间产生变化。
This is what I have come up with so far:
这就是我到目前为止所想到的:
def chessboard(n):
height = n
length = n
while height > 0:
while length > 0:
print("1", end="")
length -= 1
if length > 0:
print("0", end="")
length -= 1
height -= 1
if length == 0:
break
else:
print()
length = n
With the input:
输入:
chessboard(3)
It prints out:
它打印出:
101
101
101
Could someone help me figure out how I could start every other line with zero instead of one?
有人能帮我想办法让我每隔一行都用零而不是一开头吗?
更多回答
You should edit your question to be a minimal reproducible example. That includes your code, some input and expected output. All in text so they can be copied/pasted. It looks like 1
and 0
mean black and white? That's not immediately clear, so would be a good detail to add to your post!
你应该把你的问题编辑成一个最小的可重复的例子。其中包括您的代码、一些输入和预期输出。全部为文本,以便可以复制/粘贴。看起来1和0意味着黑色和白色?这还不清楚,所以添加到你的帖子中会是一个很好的细节!
@sc0rched that's good information to add your post, use the edit
button!
@sc0ched这是很好的信息添加你的帖子,使用编辑按钮!
Your function needs only two lines of code. First: text = '10' * n
. Second: print('\n'.join(text[:n] if line % 2 else text[1:][:n] for line in range(n)))
您的函数只需要两行代码。第一个:text='10'*n。第二个:print('\n'.join(如果第%2行为text[:n],如果第(n)行为text[1:][:n])
优秀答案推荐
As I understand it, it is simple :
据我所知,这很简单:
print("stackoverflow")
def chessboard(n):
finalSentence1 = ""
finalSentence2 = ""
for i in range(n): #we add 0 and 1 as much as we have n
if i%2 == 0: #
finalSentence1 += "1"
finalSentence2 += "0"
else:
finalSentence1 += "0"
finalSentence2 += "1"
for i in range(n): #we print as much as we have n
if i%2 == 0:
print(finalSentence1)
else:
print(finalSentence2)
chessboard(3)
returns :
退货:
stackoverflow
101
010
101
I am working on the same kind of assignment, but as we have only covered conditional statements and while loops so far, following the same logic, here is my solution:
我正在处理同样的赋值,但由于到目前为止我们只讨论了条件语句和while循环,遵循相同的逻辑,以下是我的解决方案:
def chessboard(size):
output_1 = ''
output_2 = ''
i = 1
j = 1
while j <= size:
while i <= size:
if i % 2 == 0:
output_1 += '1'
output_2 += '0'
i += 1
else:
output_1 += '0'
output_2 += '1'
i += 1
if j % 2 == 0:
print(output_1)
j += 1
else:
print(output_2)
j += 1
chessboard(5)
returns:
退货:
10101
01010
10101
01010
10101
def chessboard(x):
i = 0
while i < x:
if i % 2 == 0:
row = "10"*x
else:
row = "01"*x
print(row[0:x])
i += 1
def chessboard(n):
board = []
for i in range(n):
row = []
for j in range(n):
row.append('1' if (i+j)%2 == 0 else '0')
board.append(row)
return '\n'.join(''.join(row) for row in board)
print(chessboard(3))
Output
输出
101
010
101
更多回答
Thank you so much! This is exactly what I was looking for!
非常感谢!这正是我想要的!
I strongly advise you to understand what is written, and not only copy paste what is written :) You're welcome.
我强烈建议你理解所写的内容,而不仅仅是复制粘贴所写的:)不客气。
I will look in to how to write for loops in range straight away and how you used the remainder as an if statement.
我将研究如何直接为范围内的循环编写,以及如何将余数用作if语句。
我是一名优秀的程序员,十分优秀!