[英]How to seperate rows from columns?
I'm supposed to have a program that should ask the user to enter a number of rows and a number of columns.我应该有一个程序,应该要求用户输入多行和多列。 The program will then print a pattern based on the user's input.然后程序将根据用户的输入打印一个图案。 The pattern must follows these rules: If the row is even numbered (2nd, 4th, etc.) and the column is even-numbered, then the character in that spot will be a blank or space character.模式必须遵循以下规则:如果行是偶数(第 2、4 等)并且列是偶数,则该位置的字符将是空白字符或空格字符。 Otherwise, the character in that spot will be an asterisk: "*"否则,该位置的字符将是一个星号:“*”
This is what I have so far这是我到目前为止所拥有的
# Ask the user for the number of rows
rows = int(input('How many rows? '))
# Ask the user for the number of columns
columns = int(input('How many columns? '))
# Iterate over the rows.
for row in range("--Fill this in--"):
# Iterate over the columns.
for col in range("--Fill this in--"):
if "--Fill this in--" and "--Fill this in--":
print(' ', end='')
else:
print('*', end='')
# Go to the next row.
print()
The "fill it in" are what I do not know what to input for the program to work; “填写”是我不知道要输入什么才能使程序正常工作的内容; I know what the function is I just don't know what to put.我知道 function 是什么,只是不知道该放什么。
The output is supposed to look something like this output 应该看起来像这样
How many rows? 7
How many columns? 7
*******
* * * *
*******
* * * *
*******
* * * *
*******
Or something like this或类似的东西
How many rows? 6
How many columns? 12
************
* * * * * *
************
* * * * * *
************
* * * * * *
I was thinking maybe using an operator but I'm not really sure if an operator can be implemented here.我在想也许可以使用运算符,但我不确定是否可以在这里实现运算符。
# Ask the user for the number of rows
rows = int(input('How many rows? '))
# Ask the user for the number of columns
columns = int(input('How many columns? '))
# Iterate over the rows.
for row in range(1, rows + 1):
# Iterate over the columns.
for col in range(1, columns + 1):
if row % 2 == 0 and col % 2 == 0:
print(' ', end='')
else:
print('*', end='')
# Go to the next row.
print()
How many rows? 7
How many columns? 7
*******
* * * *
*******
* * * *
*******
* * * *
*******
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.