python - python中的棋盘功能
问题描述
我正在尝试创建一个绘制棋盘的函数,但是当我放置偶数列时它不起作用
def board(rows,columns):
for x in range(rows):
if x%2 == 0:
for y in range(1,columns+1):
if y%2 == 1:
if y != columns:
print(" ",end="")
else:
print(" ")
else:
print("|",end="")
else:
print("-"*columns)
print("True")
board(5,5)
解决方案
Inaki,问题出在列中:
if y%2 ==1:
if y != columns: # BTW you might think of y<columns
...
else:
PRINT NEW LINE. # <- you will never get here since y%2 == 1 but columns is even
您可能会喜欢这个解决方案(希望对您有所帮助!):
def board(rows,columns):
for x in range(rows*2-1):
if x%2 == 0:
print(''.join([" |"]*columns))
else:
print("--"*columns+"-")
board(4,4)
推荐阅读
- python - 解决行之间的互补缺失值
- javascript - 带有区域限制的Angular 8拖放
- python - 将 RDD 列转换为浮点数
- sql - Salesforce 营销云 SQL PIVOT
- reactjs - React Storybook 不显示 SVG 图标
- c - 在 C 中声明字符串的区别
- mysql - 循环中的 Python mysql-connector SELECT 返回过时的结果
- postgresql - 在 PostgreSQL 中存储 ISO 8601 持续时间
- python - 带有 Python 文件的随机库
- python - 我们如何处理标签在机器学习中包含 3 个序数值