How to write the following pattern - python

I am trying to print the following pattern in python but am not getting the desired output :
0
2 2
4 4 4
6 6 6 6
I have already used the following code but am not getting the desired output
n = int (input("Enter number : "))
for i in range (n):
print (i *2 )

Try this:
n = int (input("Enter number : "))
for i in range (n):
print (' '.join([str(i * 2) for _ in range(i + 1)]) )

You need to print the number i+1 times.
for i in range (n):
for _ in range(i+1):
print (i * 2," ", end='') # print without newlines
print() # end the line

Following simple code could help:
n = int (input("Enter number : "))
j = 1
k = 0
for i in range(n):
s = str(k)+' '
print(s*j)
j += 1
k += 2
if n = 4, then it would print the following :
0
2 2
4 4 4
6 6 6 6

n = int (input("Enter number : "))
count = 1
for i in range (n):
res = str(i*2) + " "
print (res * (i+1))
print n

Related

How to get the count of lines in a output of a python script

This is my code for getting a 3 digit combination.
a = input("Enter first number: ")
b = input("Enter second number: ")
c = input("Enter third number: ")
d = []
d.append(a)
d.append(b)
d.append(c)
for i in range(0, 4):
for j in range(0, 4):
for k in range(0, 4):
if(i !=j & j!=k & k!=i):
for count, i in enumerate(range(4),1):
print(i,j,k)
Input:
1,2,3
and
Output:
0 1 2
1 1 2
2 1 2
3 1 2
0 2 0
1 2 0
2 2 0
3 2 0 & more....
My question is how can I get the count of how many combinations I have got?
Thank you so much for your attention and participation.
Outside of your loop, create an integer variable and set it equal to 0. Where you print out the combinations (within the loop), add 1 to this integer variable. Finally at the end of your program outside the loop, print the value of the counter variable.
For example:
counter = 0
for i in range(0, 4):
for j in range(0, 4):
for k in range(0, 4):
if (i != j & j != k & k != i):
for count, i in enumerate(range(4), 1):
print(i, j, k)
counter += 1
print(counter)

How do I print 4 numbers in each row in python?

How do I print 4 numbers in a row? I am a complete beginner. This is Python code I did for Fibonacci sequence.
For example, if I put 6 as an input, I get
0 1 1 2 3 5
However, I want to get it like
0 1 1 2
3 5
Code:
i = 1
n1 = 0
n2 = 1
seq = n1 + n2
num = int(input("Enter a positive number: "))
while i <= num:
print(n1, end = " ")
n1 = n2
n2 = seq
seq = n1 + n2
i += 1
print()
print(n1, end = " " if i % 4 else "\n")

Print the following pattern for n number of rows. For eg. N = 5 1 1 12 21 123 321 1234 4321 1234554321

I'm doing this question but the code I wrote gives the same output with different spacing.
This is what I wrote:
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end='')
for j in range(0,2*n-2*i):
print(" ",end='')
for j in range(i,0,-1):
print(j,end='')
The output seems to be a little different then what is actually expected.
My output:
1 112 21123 3211234 43211234554321
Expected output:
1 1
12 21
123 321
1234 4321
1234554321
As I see it, with every iteration of the first for loop, we are not jumping into a new line. Why is it so? Thanks for the help.
It is a small mistake. You should add a newline printing at the last of your for loop.
Try this:
n = int(input())
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end='')
for j in range(0,2*n-2*i):
print(" ",end='')
for j in range(i,0,-1):
print(j,end='')
print('')
Tested output:
1 1
12 21
123 321
1234 4321
1234554321
A recursive solution:
def pattern(n, current=1):
if current > n:
return
r = ''.join(map(str, range(1, current+1)))
print('{: <{width}}{: >{width}}'.format(r, r[::-1], width=n))
pattern(n, current+1)
pattern(5)
Prints:
1 1
12 21
123 321
1234 4321
1234554321
Or without recursion:
def pattern(n):
for current in range(2, n+2):
r = ''.join(map(str, range(1, current)))
print('{: <{width}}{: >{width}}'.format(r, r[::-1], width=n))
pattern(5)
As pointed out by #amrs-tech, you should add a print() at the end of i-loop to get a newline, and the following also works:
def pattern(n):
agg = ""
for i in range(1, n+1):
agg = "".join((agg, str(i))
print(agg, 2*(n-i)*' ', agg[::-1]
This is a minimial solution.
You are missing a the newline being printed at the end of the outer for loop.
Alternate solution with list comprehensions:
>>> n=5
>>> for i in range(1,n):
... print(
... ''.join([str(x) for x in range(1,i+1)])
... + ' '*2*(n-i-1)
... + ''.join([str(x) for x in range(i,0,-1)])
... )
...
1 1
12 21
123 321
12344321
>>>
n = int(input('Enter number: '))
for i in range(n):
print(''.join(map(str,range(1,i+2))) + ' '*2*(n-i-1) + ''.join(map(str,range(i+1,0,-1))))
Output:
Enter number: 5
1 1
12 21
123 321
1234 4321
1234554321
n = int(input( ))
i = 1
while i <= n:
j = 1
while j <= i:
print(j, end="")
j += 1
spaces = 1
while spaces <= n - i:
print(" ", end="")
spaces += 1
space = 1
while space <= n - i:
print(" ", end="")
space += 1
j = 1
p = i
while j <= i:
print(p,end="")
j += 1
p -= 1
print()
i += 1
Easy and Clean but not optimised

logic errors with print the sum of all the numbers that are either multiple of three or five

I'm having some logic errors with my program. I've been trying to solve this for the last couple of hours. It's supposed to print the sum of all the numbers that are either multiple of three or five.
my output
1.)enter an integer number (0 to end): enter an integer number (0 to end):
2.)enter an integer number (0 to end): 3+ = 3
expected output
1.)enter an integer number (0 to end): 3 = 3
2.)enter an integer number (0 to end): 3+5 = 8
below is my code.
while True:
answer = ""
num = int(input("enter an integer number (0 to end): "))
end_answer = 0
if num == 0:
exit()
for i in range(1, num+1):
if i%3==0 or i%5==0 :
answer += str(i)
end_answer += i
if i != num and (i%3==0 or i%5==0):
answer += "+"
print(str(answer) + " = " + str(end_answer) )
I've seen similar answers for this just not in python specifically
The following (properly indented) code will give you what you need:
while True:
num = int(input('Enter an integer number (0 to end): '))
if num == 0: exit()
answer = ''
end_answer = 0
sep = ''
for i in range(1, num+1):
if i % 3 == 0 or i % 5 == 0 :
answer += sep + str(i)
sep = ' + '
end_answer += i
if end_answer > 0:
print(str(answer) + ' = ' + str(end_answer) )
Note that it uses a variable separator sep to more cleanly print the item you're working out. A sample run follows:
Enter an integer number (0 to end): 2
Enter an integer number (0 to end): 3
3 = 3
Enter an integer number (0 to end): 10
3 + 5 + 6 + 9 + 10 = 33
Enter an integer number (0 to end): 38
3 + 5 + 6 + 9 + 10 + 12 + 15 + 18 + 20 + 21 + 24 + 25 + 27 + 30 + 33 + 35 + 36 = 329
Enter an integer number (0 to end): 0
You can simplify your code a lot by using the sum builtin and f-strings for printed text formatting. This will likely be more efficient as well.
Code
from itertools import count
counter = count(1)
while True:
num = int(input(f'{next(counter)}). Enter an integer number (0 to end): '))
if num == 0:
break
nums = [x for x in range(1, num + 1) if x % 3 == 0 or x % 5 == 0]
print(f'{" + ".join(map(str, nums))} = {sum(nums)}')
Output
1). Enter an integer number (0 to end): 3
3 = 3
2). Enter an integer number (0 to end): 9
3 + 5 + 6 + 9 = 23
3). Enter an integer number (0 to end): 15
3 + 5 + 6 + 9 + 10 + 12 + 15 = 60
4). Enter an integer number (0 to end): 0

Python times table using while

Why in python a times table program doesn't work like this?
n = int(input("Type a number: "))
count = 10
while count < 0:
print(f"{count} x {n} = {n * count}")
count = count - 1
But this works correctly:
n = int(input("Type a number: "))
for i in range(1, 11):
print(f"{i} x {n} = {n * i}")
The result is (or should be), for example:
1 x 5 = 5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
As a beginner I need to understand... and is it possible to use while in that situation?
This is python 3.x
You want to start count from 1 so that your loop increments rather than decrements. Your while loop conditional check is also incorrect as count is never less than 0. Try this:
n = int(input("Type a number: "))
count = 1
while count <= 10:
print(f"{count} x {n} = {n * count}")
count = count + 1
You need to set the condition in the while to be true to go through the loop. Thus we will say do while count is greater than or equal to zero.
n = int(input("Type a number: "))
count = 10
while count >= 0:
print(f"{count} x {n} = {n * count}")
count = count - 1

Categories