How to print this pattern? I have done regular patterns but I am unable to get odd stars.
You could use a simple list comprehension:
n = 5
print("\n".join((a := [("* "*i).center(n*2 + 1) for i in range(1, n + 1) if i&1]) + a[::-1][1:]))
*
* * *
* * * * *
* * *
*
You can change n to anything you want
Related
This is my code which i wrote in python
n = 5
row = 2 * n - 2
for i in range(n,-1,-1):
for j in range(row):
print(end="")
row -=2
for k in range(i+1):
print('*',end=" ")
print()
The output what is get is this
* * * * *
* * * *
* * *
* *
*
i want to print this start from left to right order for example
The expected output is :-
* * * * *
* * * *
* * *
* *
*
if it's any possible way to print the elements from left to right because in most of my program i need that logic i'm searching for it please help me and even i used reversed function for loop it will reverse the loop but i'm not getting what i expect
n = 5
print(*[' '.join(' '*i + '*'*(n-i)) for i in range(n)], sep='\n')
Output:
* * * * *
* * * *
* * *
* *
*
Explanation:
for i in range(n):
chars = ' '*i + '*'*(n-i) # creating list of (i) spaces followed
# by (n-i) stars to finish a line of n elements
print(' '.join(chars)) # join prepared values with spaces
Here is a simple solution not using list comprehension:
n = 5
for i in range(n+1):
for j in range(i):
print(" ", end="")
for j in range(i+1, n+1):
print("* ", end="")
print()
Output:
* * * * *
* * * *
* * *
* *
*
My solution - I'm new to python as well:
n = 5
c = 0
for i in range(n, 0, -1):
print(" " * c + "*" * i)
c += 1
or
n = 5
c = 0
while n >= 0:
print(" " * c + "*" * n)
n -= 1
c += 1
The problem is that print itself cannot print right-justified text. But you can print a right-justified string instead of using multiple calls to print.
Here's your original code, using join instead of an inner loop:
n = 5
row = 2 * n - 2
for i in range(n,-1,-1):
for j in range(row):
print(end="")
row -=2
row_str = " ".join(["*"] * i)
print(row_str)
And here's the modified code to make the output right-justified:
n = 5
row = 2 * n - 2
whole = row + 1
for i in range(n,-1,-1):
for j in range(row):
print(end="")
row -=2
row_str = " ".join(["*"] * i).rjust(whole)
print(row_str)
How can I make a perfect Christmas tree in python? i have here my code but its not working well, it needs to print '~~' in the first for loop.
height = 7
for a in range(1, (height + height) - 3):
if a % 2 != 0:
if a == 1:
print(a * 'o')
else:
print(a * '* ')
for a in range(height + 1):
if a % (height + 1) == 1:
test = height - 3
print(test * '~~' + a * '|' + test * '~~')
if a % (height + 1) == 1:
test = height - 3
print(test * '~~' + a * '|' + test * '~~')
Output of my code is:
o
* * *
* * * * *
* * * * * * *
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~
my desired output is:
~~~~~~~~o~~~~~~~~
~~~~~~* * *~~~~~~
~~~~* * * * *~~~~
~~* * * * * * *~~
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~
def values():
yield 'o'
for i in range(3, 10, 2):
yield ' '.join('*' * i)
yield from '||'
for v in values():
print('{:~^17}'.format(v))
Prints:
~~~~~~~~o~~~~~~~~
~~~~~~* * *~~~~~~
~~~~* * * * *~~~~
~~* * * * * * *~~
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~
Or:
for v in ['o'] + [' '.join('*' * i) for i in range(3, 10, 2)] + ['|', '|']:
print('{:~^17}'.format(v))
Here is an example using the string method center.
for x in range(1, 30, 2):
s = '*' * x
print(s.center(30))
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
>>>
EDIT: Including the tildes.
limit = 30
for x in range(1, limit, 2):
tildes = '~' * ((limit-x)//2)
out = tildes + '*' * x + tildes
print(out)
~~~~~~~~~~~~~~*~~~~~~~~~~~~~~
~~~~~~~~~~~~~***~~~~~~~~~~~~~
~~~~~~~~~~~~*****~~~~~~~~~~~~
~~~~~~~~~~~*******~~~~~~~~~~~
~~~~~~~~~~*********~~~~~~~~~~
~~~~~~~~~***********~~~~~~~~~
~~~~~~~~*************~~~~~~~~
~~~~~~~***************~~~~~~~
~~~~~~*****************~~~~~~
~~~~~*******************~~~~~
~~~~*********************~~~~
~~~***********************~~~
~~*************************~~
~***************************~
*****************************
I have modified your code a little bit, well here's the code:
height = 7
z = height - 3
x = 1
for i in range(1, (height + height) - 3):
if i % 2 != 0:
if(i==1):
print('~~' * z + 'o' +'~~' * z)
else:
print('~~' * z + '* ' * (x-1)+ '*' *1 +'~~' * z)
x+=2
z-=1
for a in range(height + 1):
if a % (height + 1) == 1:
test = height - 3
print(test * '~~' + a * '|' + test * '~~')
if a % (height + 1) == 1:
test = height - 3
print(test * '~~' + a * '|' + test * '~~')
Output:
~~~~~~~~o~~~~~~~~
~~~~~~* * *~~~~~~
~~~~* * * * *~~~~
~~* * * * * * *~~
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~
I edited your code just a little:
height = 7
width = 17
for a in range(1, (height + height) - 3):
if a % 2 != 0:
if a == 1:
sym = 'o'
curly = ''.join(['~' for i in range((width-len(sym))//2)])
print(curly + a * sym + curly)
else:
sym = a * '* '
curly = ''.join(['~' for i in range((width - len(sym)) // 2)])
print(curly + sym + curly)
for a in range(height + 1):
if a % (height + 1) == 1:
test = height - 3
print(test * '~~' + a * '|' + test * '~~')
if a % (height + 1) == 1:
test = height - 3
print(test * '~~' + a * '|' + test * '~~')
~~~~~~~~o~~~~~~~~
~~~~~* * * ~~~~~
~~~* * * * * ~~~
~* * * * * * * ~
* * * * * * * * *
~~~~~~~~|~~~~~~~~
~~~~~~~~|~~~~~~~~
It's not perfect but no Christmas tree is 100% straight.
It is a simple programming problem. So let's create a simple program for solving it.
First, we need to draw our tree. in each line, our tree has an odd number of starts for showing tree and some ~ characters for representing the background. If we want to have a tree which its height is equal to the height variable. at the heightth line, we don't want to have any ~ character. But in the first line, we want to have only one * character.
So it seems to be a good idea to having a for loop that counts from height to the zero and relates the number of ~ characters somehow to it.
Let's see the code first:
for i in range(height, 0, -1):
print("~" * (i - 1)) // repeating `~` character (i - 1) times
If you run this code, you will get the following output:
~~~~~~
~~~~~
~~~~
~~~
~~
~
Now let's add the * characters for drawing the tree. We should have the exact opposite relation for * characters from ~.
for i in range(height, 0, -1):
print("~" * (i - 1), end="")
print("*" * (((height - i) *2) + 1))
We add end="" at the end of the previous print function. Because print will add a newline character at the end in each call and we want to prevent it (because we want to add * characters after ~ characters).
Then we want to add * characters at each line. But we want to count their number
for 1 to height * 2. But we only need the odd numbers. That's what we did in the second line. Now, we have the following number of * characters at each line: 1, 3, 5, 7,...
Below is the output of that code (We considered that height = 7):
~~~~~~*
~~~~~***
~~~~*****
~~~*******
~~*********
~***********
*************
As you can see, now we have our Christmas tree. But our background is incomplete.
The only thing we should do is to repeat our first print statement. But this time we should not add end="" because we want that newline at the end of each line.
for i in range(height, 0, -1):
print("~" * (i - 1), end="")
print("*" * (((height - i) *2) + 1), end="")
print("~" * (i - 1))
Now our tree is complete:
~~~~~~*~~~~~~
~~~~~***~~~~~
~~~~*****~~~~
~~~*******~~~
~~*********~~
~***********~
*************
You can add other things to your tree by following this simple approach. I hope this helps you.
Try using more conditionals to handle the number of stars (*). Drop a comment below if that doesn't work.
Like, you could do:
if '1star' do this..:
elif '2stars' do this..:
I would code it like this:
sky=8
tree=1
for i in range(0,5):
if i==0:
print(sky*'~'+tree*'o'+sky*'~')
else:
print(sky*'~'+tree*'*'+sky*'~')
sky-=1
tree+=2
sky=8
tree=1
for i in range(0,2):
print(sky*'~'+tree*'|'+sky*'~')
I modified the Christmas tree so that it looked thinner.
This is my code:
var = 14
for i in range(1, var + 1):
print(" " * (var - i) + "*" * i + "*" * (i - 1))
Sample output:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
I m trying to print the following triangle in python, but not able to
*#####
**####
***###
****##
*****#
I am only able to print '*', but not hashes.
here's my code to print '*'
max = 6
for x in range(1,max+1):
for y in range(1,x+1):
print '*',
print
length = 6
for x in range(1, length+1):
print(x * '*' + (length - x) * '#') # integer * (string), concatenate the string integer times
How to Print * in a triangle shape in python?
*
* *
* * *
* * * *
* * * * *
This is one of the coding interview questions.
def print_triangle(n):
for i, j in zip(range(1,n+1), range(n,0,-1)):
print(" "*j, '* '*i, " "*j)
print_triangle(5)
This question already has answers here:
How to recreate pyramid triangle?
(3 answers)
Closed 5 years ago.
I am trying to print a triangle made of asterisk (*) separated by spaces.
If n = 4, it should look like:
*
* *
* * *
* * * *
This is the code I have:
n = 4
for i in range(1, n + 1):
for j in range(i):
print("*")
This is the result I get:
*
*
*
*
*
*
*
*
*
*
I would very appreciate what is wrong with my code...
print() adds a newline to your string each time.
It is easier to multiply the * with the number of times you would like to see it:
n = 4
for i in range(1, n + 1):
print("* " * i)
Output:
*
* *
* * *
* * * *
You could use the built-in "end" parameter:
n = 4
for i in range(1, n + 1):
for j in range(i):
print("*", end=" ")
print()
n = 4
for i in range(1, n + 1):
lvl = ""
for j in range(i):
lvl += "* "
print(lvl)
You need to aggregate the level of the triangle to print.
edit :
if you don't want spaces at the end of the line :-/
n = 4
for i in range(1, n+1):
print( " ".join([c for c in '*' * i]))
I am having trouble to get my function to work. My functions needs to be a parameter which accepts *Asterisk characters and will print out 1 asterisk, 3 ast., 5, any odd numbers in the subsequent lines. So first line will have 1 ast, second line 3 ast., and so on. The parameter will accept any odd int.
My attempt:
def arrowHead(n):
spaces = n / 2
for x in range(1, n+2, 2):
string_ln = ''
for num in range(spaces):
string_ln = string_ln + ' '
for num2 in range(x): string_ln = string_ln + '*'
spaces = spaces - 1
print string_ln
final_string = ''
for x in range(n / 2):
final_string = final_string + ' '
final_string = final_string + '*'
for x in range(3):
print final_string
You're not going to be able to print an arrow head this way, because you can't print characters in between spaces!
You could try something like this?
def arrow(n):
for x in range(n+1):
print (' '*n)+(' *'*x)
n -= 1
>> arrow(7)
>>
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
If you want to debug a Python script, you can use pdb. For example, in your system's shell or command line:
python -m pdb yourscript.py
You can then step through your script using n (which skips function calls), s (which steps into function calls), print the state of variables using p yourvarname, and use several other commands (try using help to see them all and then help somecommand to see detailed help).
Not clear with your question, but if you want to print asterisk odd number of times, go with this
def arrowHead(n):
print '*' * (2*n-1)
Update:-
>>> def arrow(n,flag=1):
if flag<n:
print (' '*(arrow(n,flag+1))+' * '*(n-(2*flag-1)))
return 2*flag-1
>>> arrow(10)
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *