These are what I'm currently working with:
row = int(input('Enter number of rows required: '))
for i in range(row,0,-1):
for j in range(row-i):
print('*', end='')
for j in range(2*i-1):
print('0',end='')
print()
And this is the current output:
0000000
*00000
**000
***0
I can't figure out what to change to have the correct output
Add another asterisk-printing loop before printing a newline.
row = 4
for i in range(row, 0, -1):
for j in range(row - i):
print('*', end='')
for j in range(2 * i - 1):
print('0', end='')
for j in range(row - i):
print('*', end='')
print()
prints out
0000000
*00000*
**000**
***0***
(For a more Pythonic solution, you could just
row = 4
full_width = 2 * row - 1
for i in range(row, 0, -1):
row_zeroes = (2 * i - 1)
print(("0" * row_zeroes).center(full_width, '*'))
but I suspect that's not the point of this exercise.)
you can use f-string and siple nth number from the formula a + (n-1)*d
row = int(input('Enter number of rows required: '))
for i in range(row):
print(f'{"*"*(i+1)}{"0"*(2*(row-i-1) - 1)}{"*"*(i+1)}')
Related
I am writing a code to generate a pascal's triangle, and everything works except for the first '1' in the sequence is not generating.
rows = input("Please state the number of rows you would like your triangle to have: ")
rows = int(rows)
for i in range(rows):
value = 1
for j in range(0, rows - i + 1):
print(end=" ")
for j in range(1, i + 1):
value = value * (i - j + 1) / j
value = int(value)
print(value, end=" ")
print()
what it prints:
1
2 1
3 3 1
4 6 4 1
You can change your inner loop to start at 0, and then check to make sure you're not dividing by 0:
for i in range(rows):
value = 1
for j in range(0, rows - i + 1):
print(end=" ")
for j in range(i + 1):
value = (value * (i - j + 1) / j) if j else 1
value = int(value)
print(value, end=" ")
print()
This question already has answers here:
print a diamond of numbers in python
(3 answers)
Closed 1 year ago.
When n = 5 the pattern must be:
1
121
12321
121
1
What I tried till now:
# Pattern 1-121-12321 pyramid pattern
# Reading number of rows
row = int(input('Enter how many lines? '))
# Generating pattern
for i in range(1,row+1):
# for space
for j in range(1, row+1-i):
print(' ', end='')
# for increasing pattern
for j in range(1,i+1):
print(j, end='')
# for decreasing pattern
for j in range(i-1,0,-1):
print(j, end='')
# Moving to next line
print()
Output I am getting:
1
121
12321
1234321
123454321
I know the logic for similar such patterns like:
*
***
*****
*******
But the number pattern seems to be confusing and I am not able to get the logic.
TL;DR
Python Program for generating the following Pattern for printing n rows:
eg. When n=7:
1
121
12321
1234321
12321
121
1
How about this code:
for i in range(n):
temp = 0 # Store the value of the number to square (so 1, 11,...)
if (i < n / 2):
for j in range(i + 1):
temp += 10 ** j # Construct the number (1 = 1, 11 = 1 + 10, 111 = 1 + 10 + 10 ^ 2,...)
for _ in range(int(n / 2 - i)):
print(' ', end = '') # Add space indentation
else:
for j in range(n - i): # Count in reverse now
temp += 10 ** j # Construct the number (1 = 1, 11 = 1 + 10, 111 = 1 + 10 + 10 ^ 2,...)
for _ in range(int(i - n / 2) + 1):
print(' ', end = '') # Add space indentation
print(temp ** 2) # Square the temporary value
(Fun mathematical fact: the string you print have a property of:
1 = 1 ^ 2; 121 = 11 ^ 2; 12321 = 111 ^ 2;...)
I will also give my suggestion. So here I initially just outsource the production of the line into a method. I first calculate the length of the current line and depending on the maximum number of lines you want to print determine the padding to get this star shape. Then I produce the actual string, which is dependent on the middle point of the string, so as soon as I exceed this, I decrease the numbering again.
from math import ceil
def produce_line(line, maximum):
middle = ceil(maximum / 2)
padding = abs(middle - line)
line_width = maximum - 2 * padding
half_lw = ceil(line_width / 2)
res = (" " * padding) + "".join([str(a) if a <= half_lw else str(abs(2 * half_lw - a)) for a in range(1, line_width + 1)])
return res
lines = 9
for i in range(1, lines + 1):
print(produce_line(i, lines))
You could add an if statement and start from a count with a new variable c:
row = int(input('Enter how many lines? '))
c = row // 2
for i in range(1,row+1):
if i <= (row // 2 + 1):
# for space
for j in range(1, row+1-i):
print(' ', end='')
# for increasing pattern
for j in range(1,i+1):
print(j, end='')
# for decreasing pattern
for j in range(i-1,0,-1):
print(j, end='')
else:
for j in range(1, i):
print(' ', end='')
# for increasing pattern
for j in range(1, c):
print(j, end='')
# for decreasing pattern
for j in range(c, 0, -1):
print(j, end='')
c -= 1
# Moving to next line
print()
Output:
Enter how many lines? 7
1
121
12321
1234321
12321
121
1
Here is a short solution with a single loop an without test, it generates the range/string only once, all the rest is calculating the indices and slicing:
n = 5
n2 = (n+3)//2 # half length +1 e.g. 4 for n=5
s = ''.join(map(str,range(1, n2))) # generate '123' for n=5
for i in range(n):
stop = n//2-abs(i-n//2) # calculate length of half-string 0,1,2,1,0 for n=5
S = s[:stop+1] # slice string
print(S.rjust(n2)+S[-2::-1]) # print line e.g. '123' + '21'
output:
1
121
12321
121
1
You can try it:
for i in range(1, row+1):
if i > (row+1)/2:
i = (row+1)-i
# for space
for j in range(1, row+1-i):
print(' ', end='')
# for increasing pattern
for j in range(1, i+1):
print(j, end='')
# for decreasing pattern
for j in range(i-1, 0, -1):
print(j, end='')
# Moving to next line
print()
input:
9
output:
1
121
12321
1234321
123454321
1234321
12321
121
1
input:
7
output:
1
121
12321
1234321
12321
121
1
You can also try to simplify the problem be writing a function to get one line. And when you have that working, calling this for each line.
def create_line(number, spaces):
return ''.join([' ' for s in range(spaces)] +
[str(x) for x in range(1, number)] +
[str(x) for x in range(number, 0, -1)] +
[' ' for s in range(spaces)])
def print_pattern(n):
highest_number = (n + 1) // 2
for line_no in range(1, n + 1):
print(create_line(highest_number - abs(highest_number - line_no),
abs(highest_number - line_no)))
row = int(input('Enter how many lines? '))
print_pattern(row)
With the same approach you have taken if you have divided the problem into two as first half and second half you can generate the pattern easily with your initial approach. I have provided an easy way to break your problem like below.
# Reading number of rows
row = int(input('Enter how many lines? '))
def firsthalf(rows):
for i in range(1, rows + 1):
# for space
for j in range(1, rows + 1 - i):
print(' ', end='')
# for increasing pattern
for j in range(1, i + 1):
print(j, end='')
# for decreasing pattern
for j in range(i - 1, 0, -1):
print(j, end='')
# Moving to next line
print()
def bottomHalf(rows):
for i in range(rows, 0, -1):
# for space
for j in range(1, rows + 2 - i):
print(' ', end='')
# for increasing pattern
for j in range(1, i + 1):
print(j, end='')
# for decreasing pattern
for j in range(i - 1, 0, -1):
print(j, end='')
def pattern(rows):
firsthalf((rows // 2) + 1)
bottomHalf(rows // 2)
pattern(row)
I'm a beginner in python and I've been trying to print this for the past 40 minutes. Any help on how I can do this would be greatly appreciated.
The first line has 11 spaces, the second has 7 spaces, the third has 3 spaces, and the last has no spaces.
for row in range(4):
for column in range(row + 1):
print(column, end='')
print("")
So far I have two loops. This one prints: (image)
The other one is: n = 3 k = 3 for i in range(0,n+1): for j in range(k-i, -1, -1): print(j, end='') print()
That prints:
this. stack.imgur.com/IAIHk.png
I'm stuck on what to do, but I think I have a general idea. Please help!
This should work if you use a monospaced font
for i in range(1, 5):
j = 0
spaces = 2 * (4 - i) - 1
while j < i:
print(j, end = "")
j += 1
if spaces == -1:
j -= 1
else:
print(" " * spaces, end = "")
while j > 0:
j -= 1
print(j, end = "")
print()
If you want this work for any number you can define a function like
def f(n):
for i in range(1, n + 1):
j = 0
spaces = 2 * (n - i) - 1
while j < i:
print(j, end = "")
j += 1
if spaces == -1:
j -= 1
else:
print(" " * spaces, end = "")
while j > 0:
j -= 1
print(j, end = "")
print()
For example, f(6) will print
0 0
01 10
012 210
0123 3210
01234 43210
01234543210
Hope I helped
I am new on Python and I am following a book that purposes the following excercise:
Write a program to generate the following pattern in Python:
*
**
***
****
The suggested code is:
n = input('Enter the number of rows: ')
m = int(n)
*k = 1
for i in range(m):
for j in range(i, i + 2):
print('*', end = " ")
print()
and enter n=5.This lead me to ask to questions. The first one is the *k=1, I am asumming the '' is a typo on the book since my program does not run with it. However i am not seeing that k is being used within the loop. My second question is that I do not understand why my outcome is different than the one I get (once removed the ''). This is what I get when n=5:
**
**
**
**
**
This works for your problem. In python, you can multiply strings. It will be useful for the problem. *k is a typo.
n = input(' num rows: ')
n = int(n)
for i in range(1, n + 1):
print ('*' * i)
You can try this solution. I am pretty sure the *k = 1 is a typo.
n = int(input('Enter the number of rows: '))
k = 1
for i in range(n):
print(k * '* ', end = "\n")
k += 1
Another approach if you don't want to use multiplication approach:
n = input('Enter the number of rows: ')
m = int(n)
for i in range(m):
for j in range(1, i + 2):
print('*', end = " ")
print()
def printStarsInTriangeForm(count):
for i in (range(1, count + 1)):
print("*" * i)
This is one way. :)
yea *k = 1 cant be right, you can delete it.
Your mistake or the mistake in the book is the line:
for j in range(i, i + 2):
if you type i * 2 it works:
for j in range(i, i * 2):
and if you want no spaces between the starts you need to change the print in the loop to:
print("*",end="")
i just removed the space in the "end".
and a better way you can do this is
m = int(input('Enter the number of rows: '))
for i in range(m + 1):
print(i * "*")
I am trying to write a program that looks something like this if, say, the input number was 6, or something like that the output should look like this:
*
**
***
****
*****
******
*****
****
***
**
*
but when I do it like I was told, this way specifically because this is what a classmate told me to do. :
n = int(input("Enter a value for n: "))
for i in range(1, n + 1):
for j in range(n):
if n - j > i:
print(" ", end = " ")
else:
print("*", end = " ")
print("")
for i in range(1, n):
for j in range(n):
if n - j < i:
print(" ", end = " ")
else:
print("*", end = " ")
print("")
I get:
*
**
***
****
*****
******
*****
****
***
**
*
What am I doing wrong? Please tell me how to get it to correctly line up, I'd really appreciate it if someone could help me with this so I can learn to do this on my own, please assist me...
If your assignment requires you to write the code exactly as you posted, Austin Kootz answer is the way to go.
However, a more simplifed way of doing this is using ljust
n = 6
for x in range(n - 1, 0, -1):
print ''.ljust(x, ' ') + '*'.ljust(n - x, '*')
for x in range(n):
print ''.ljust(x, ' ') + '*'.ljust(n - x, '*')
Your loops are a bit overcomplicated, so I've simplified somewhat:
n = int(input("Enter a value for n: "))
for x in range(n):
out = ''
for y in range(n-x):
out = out +' '
for y in range(x):
out = out +'*'
print(out)
for x in range(n):
out = ''
for y in range(x):
out = out +' '
for y in range(n-x):
out = out +'*'
print(out)
Enjoy!
What you want in the second set of loops is to take the row number (counting from 1) and print that many spaces (" "), and then print asterisks ("*") for the rest of the row. So if i is the row number and j the column number (and indexing starts from 0), you should print " " while j < i + 1 and "*" otherwise. This gives:
# The top part of the pyramid
for i in range(1, n + 1):
for j in range(n):
if n - j > i:
print(" ", end = " ")
else:
print("*", end = " ")
print("")
# The bottom half of the pyramid
for i in range(n):
for j in range(n):
# Print spaces in the beginning of the row
# (while the column number is less than the row number)
if j < i + 1:
print(" ", end = " ")
# Print asterisks for the rest of the row
else:
print("*", end = " ")
print("")