Printing Simple Pattern in Python - python

I would like to print the following pattern in Python
input: 5
output:
5
456
34567
2345678
123456789
I have used the following code but it is not showing the above pattern. Anyone help me on this topic, please?
CODE:
rows = int(input("Enter number of rows: "))
k = 0
count=0
count1=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(" ", end="")
count+=1
while k!=((2*i)-1):
if count<=rows-1:
print(i+k, end=" ")
count+=1
else:
count1+=1
print(i+k-(2*count1), end=" ")
k += 1
count1 = count = k = 0
print()
OUTPUT:
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

If I understand your question correctly, you just want a pattern starting from n and going to 1 in decreasing order left side, and starting from n and going to 2n-1 in increasing order right side
def pattern(n):
for i in range(n,0,-1):
for j in range(1,i):
print(" ",end="")
for k in range(i,2*n-i+1):
print(k,end="")
print()
pattern(5)
5
456
34567
2345678
123456789

Related

User input matrix but wrong output using phython

so basically, I want to make an automatic matrix input and generate the output but the second part of the input shows up with the same output as the first one. is there any wrong logic behind my code? here's my code (im sorry for bad english):
#input menu 2 :
def prgmatriks2() :
ct = 2
for a in range(ct):
m = int (input("input row "+str (a)+": "))
n = int (input("input column"+str (a)+": "))
# initialization matriks
matrix = []
print("Matriks input: ")
#Input :
for x in range(ct) :
for i in range(m): #rows loop
a =[]
for j in range(n): #column loop
a.append(int(input()))
matrix.append(a)
# Print Matriks
for x in range(ct) :
for i in range(m):
for j in range(n):
print(matrix[i][j], end = " ")
print()
and for the output:
output
text version:
Pilihan menu(1-2):
2
input row 0: 2
input column0: 2
input row 1: 2
input column1: 2
Matriks input:
3
2
4
1
6
5
2
1
3 2
4 1
3 2
4 1
instead generate :
3 2
4 1
6 5
2 1
the program repeat the first output :
3 2
4 1
3 2
4 1
print(matrix[i][j], end=" ") replace here i with i+x*n.
So logic for printing Matrices will be like this code :
# Print Matrices
for x in range(ct):
for i in range(m):
for j in range(n):
print(matrix[x*n+i][j], end=" ")
print()
print()
print(matrix)
OUTPUT :
3 2
4 1
6 5
2 1

How do I modify this code so that it will print the pattern correctly?

for i in range(2):
for j in range(1,11):
print(j," ",end="")
print()
I need to print this pattern, but I can't figure how to get it to subtract 1 on the next line. Please help.
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
Is this what you meant?
for i in range(2):
for j in range(1, 11):
print(j - i, end=" ")
print()
Another way, just for fun :) would be limiting the range of j with current value of i:
>>> for i in range(1, -1, -1):
for j in range(i, 10+i):
print (j, end=" ")
print()
1 2 3 4 5 6 7 8 9 10
0 1 2 3 4 5 6 7 8 9
OR
>>> for i in range(2):
for j in range(1-i, 11-i):
print (j, end=" ")
print()
Using list comprehension you can do like this also: This is different approach.
print ("\n".join([" ".join([str(j) for j in range(1,11)])] + [" ".join([str(i-1) for i in range(1,11)])]))
for i in range(2):
for j in range(1,11):
print(j - i," ",end="")
if you want results in one line try this

Print with end=' ', proper formatting?

Code
fav_numbers = {
'bill': [4,6,7,1],
'uros': [9,0,1,2],
'pera': [1,6,3,5],
}
for name, numbers in fav_numbers.items():
print(name.title())
for num in numbers:
print(num, end=' ')
Result
Bill
4 6 7 1 Uros
9 0 1 2 Pera
1 6 3 5 [Finished in 0.1s]
Question
How do I write the for loop so that the last two names are formatted properly?
The problem is not the end=' ', but the print(name.title()) that's missing it. Add end= ' to that, too, to print the numbers into the same line, then finish with a print() for the final linebreak.
for name, numbers in fav_numbers.items():
print(name.title(), end=' ') # no newline here
for num in numbers:
print(num, end=' ')
print() # but newline here
Alternatively, you can use print without special parameters and pass the numbers as var-arg parameter using *.
for name, numbers in fav_numbers.items():
print(name.title(), *numbers)
This prints the name and the individual numbers, all separated with spaces. Both ways, the output is:
Pera 1 6 3 5
Bill 4 6 7 1
Uros 9 0 1 2
Just add another print at the end of the loop to start in a new line:
for name, numbers in fav_numbers.items():
print(name.title())
for num in numbers:
print(num, end=' ')
print()
Here have it live
fav_numbers = {
'bill': [4,6,7,1],
'uros': [9,0,1,2],
'pera': [1,6,3,5],
}
for k,v in fav_numbers.items():
print(k, *v)
Output
bill 4 6 7 1
uros 9 0 1 2
pera 1 6 3 5
format is great for printing in a specific format
for name,numbers in fav_numbers.items():
print('{}: {}'.format(name,' '.join((str(n) for n in numbers))))
bill: 4 6 7 1
uros: 9 0 1 2
pera: 1 6 3 5
fav_numbers = {
'bill': [4,6,7,1],
'uros': [9,0,1,2],
'pera': [1,6,3,5],
}
for name,numbers in fav_numbers.items():
print(", ".join( repr(e) for e in numbers ).replace(',',''),name)
Output
4 6 7 1 bill
9 0 1 2 uros
1 6 3 5 pera

For loop a square pattern of numbers

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
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
Simple solution: Just use nested for loops:
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 ('\n')
for x in range(rows):
print (i + 1)
The code above will go through the range 0 to num, printing first a new line and then printing the current number rows times.
rows = 5
side = int(input("Please Enter any Side of a Square : "))
for i in range(side):
for j in range(side):
if(i == 0 or i == side - 1 or j == 0 or j == side - 1):
print(i, end = ' ')
else:
print(i, end = ' ')
print()

write a program that prints a nested loop in Python

I'm trying to print a nested loops that looks like this:
1 2 3 4
5 6 7 8
9 10 11 12
This is what I have so far:
def main11():
for n in range(1,13)
print(n, end=' ')
however, this prints the numbers in one line: 1 2 3 4 5 6 7 8 9 10 11 12
You can do that using string formatting:
for i in range(1,13):
print '{:2}'.format(i),
if i%4==0: print
[OUTPUT]
1 2 3 4
5 6 7 8
9 10 11 12
Modulus Operator (%)
for n in range(1,13):
print(n, end=' ')
if n%4 == 0:
print
for offset in range(3):
for i in range(1,5):
n = offset*4 + i
print(n, end=' ')
print()
Output:
1 2 3 4
5 6 7 8
9 10 11 12
Or if you want it nicely formatted the way you have in your post:
for offset in range(3):
for i in range(1,5):
n = offset*4 + i
print("% 2s"%n, end=' ')
print()
Output:
1 2 3 4
5 6 7 8
9 10 11 12
Most of the time when you write a for loop, you should check if this is the right implementation.
From the requirements you have, I would write something like this:
NB_NB_INLINE = 4
MAX_NB = 12
start = 1
while start < MAX_NB:
print( ("{: 3d}" * NB_NB_INLINE).format(*tuple( j+start for j in range(NB_NB_INLINE))) )
start += NB_NB_INLINE

Categories

Resources