简体   繁体   English

如何将行与列分开?

[英]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.

相关问题 从单独的行/列中减去/相加 - Subtraction/Addition from seperate rows/columns 如何从两个单独的列中减去时间戳,然后将此数据输入表中 - How to subtract timestamps from two seperate columns, then enter this data into the table 如何更具体地分隔行? - How to seperate rows more specifically? 熊猫:根据单独的列的值更新多个列和行 - Pandas: Updating multiple columns and rows based on the value of a seperate column 使用 pandas 在单独的列中获取具有相同值组合的行 - Get rows with same value combination in seperate columns using pandas 如何将行组合成单独的 dataframe python pandas - How to combine rows into seperate dataframe python pandas 如何在单独的列中写入csv文件? - How to write to csv file in seperate columns? 如何为同一个pandas Dataframe的所有不同列制作单独的Seaborn kdeplots? - How to make seperate Seaborn kdeplots for all different columns from the same pandas Dataframe? 如何计算来自不同列的两个字符串之间的归一化编辑相似度 - How to calulate the normalized editex similarity between two strings from seperate columns 如何在tkinter GUI的不同行上输出来自sqlite3查询的结果 - How to output results from a sqlite3 query on seperate rows in a tkinter GUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM