3loosh - 10 months ago 45

Python Question

In this question I had to create a program that prompts the user for a number and then prompt again for how many rows to create. Something like:

`1 1 1 1 1 1 1`

2 2 2 2 2 2 2

3 3 3 3 3 3 3

4 4 4 4 4 4 4

This is what I came up with and I have tried many different ways to get the same result but it didn't work.

`num=int(input("Enter a number between 1 and 10: "))`

rows=int(input("Enter how many rows to of numbers: "))

for i in range(num):

print(i,end=" ")

for x in range(rows):

print (x)

This is the output I came up with:

`Enter a number between 1 and 10: 6`

Enter how many rows to of numbers: 4

0 1 2 3 4 5 0

1

2

3

Answer Source

You may simply do it like:

```
num = 5
rows = 4
for i in range(1, num+1):
print('{} '.format(i) * rows)
```

Output:

```
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
```

*Explanation:* When you multiply a `str`

with some number say `n`

, new string is returned with original string repeated `n`

times. Doing this you will eliminate your nested loop