用户输入乘以行?

时间:2017-10-24 20:44:41

标签: python

我正在尝试编写一个程序,提示用户为行和列输入2个数字。然后用星号输出网格形状但在for循环内。

userrow = int(input("Enter the size of the row:"))
usercolumn = int(input("Enter the size of the column: "))

row = 0
column = 0

for row in ('*'):
    print(row * userrow)

    for column in ('*'):
        print(column * usercolumn )

#gridlayout = row * column
print("The grid layout is: ", row,  column)

2 个答案:

答案 0 :(得分:3)

我将首先解释当前代码的作用,错误原因以及如何重写代码。

row

这会将名为'*'的变量迭代到row = '*' print(row * userrow) for column in '*': print(column * usercolumn) 。由于这是一个大小为1的可迭代,因此它实际上不会循环。所以你的代码基本上是:

row = '*'
print(row * userrow)
column = '*'
print(column * usercolumn)

对于专栏也是如此:

userrow

这只会打印usercolumn星号,然后打印userrow星号。您想要做的是循环usercolumn次或for i in range(usercolumn): print('*' * userrow) 次:

usercolumn

这样可行,打印userrow星号,print('\n'.join(['*'*usercolumn]*userrow))次。或者, <UserControl.Resources> <Style BasedOn="{StaticResource MaterialDesignSnackbarActionButton}" x:Key="SnackbarThemeChangedStyle" TargetType="Button"> <Setter Property="Foreground" Value="{DynamicResource SecondaryAccentBrush}"/> </Style> </UserControl.Resources> ..... <materialDesign:Snackbar x:Name="SnackbarMaximumCharacters" HorizontalAlignment="Stretch" VerticalContentAlignment="Bottom" ActionButtonStyle="{StaticResource SnackbarThemeChangedStyle}" IsActive="True"> <materialDesign:SnackbarMessage Content="The maximum number of characters is 15" ActionContent="OK" ActionClick="SnackbarMessage_ActionClick"/> </materialDesign:Snackbar>

答案 1 :(得分:1)

你好,这会做的吗?不需要2个循环。也没有必要制作2个相同的变量(行和用户)

userrow = 2
usercolumn = 3

for i in range(0,usercolumn):
    print(userrow * '*')

print("The grid layout is: ", userrow,  usercolumn)

输出:

**
**
**
The grid layout is:  2 3