I wanna create a Christmas tree in python with layer and base no random just print Christmas
For example, if I enter the number 3 the program should print:
I wanna output like this
*
***
*
***
*****
*
***
*****
*******
|
===V===
Code:
a = int(input(''))
for i in range(a, 0, -1):
for j in range(0, 5):
print(end=' ')
for f in range(0, i):
print(end=' ')
for h in range(0, 2 * (a - i) + 1):
print('*', end='')
print()
b = a + 1
for i in range(b, 0, -1):
for j in range(0, 2):
print(end=' ')
for f in range(0, i):
print(end=' ')
for h in range(0, (2 * (b - i) + 1)):
print('*', end='')
print()
c = a + 2
for i in range(c, 0, -1):
for j in range(0, 1):
print(end=' ')
for f in range(0, i):
print(end=' ')
for h in range(0, (2 * (c - i) + 1)):
print('*', end='')
print()
d = a + 3
for i in range(d, 0, -1):
for f in range(0, i):
print(end=' ')
for h in range(0, (2 * (d - i) + 1)):
print('*', end='')
print()
It does not work because when I ran this code program print like this
*
***
*****
*
***
*****
*******
*
***
*****
*******
*********
*
***
*****
*******
*********
***********
I don't know why can you guy help me?
[1]: https://i.stack.imgur.com/lMtMB.png
Try this:
n = int(input())
addSpace = 4
for i in range(1, n+1):
left = " "*(n-i+addSpace)
for j in range(i+1):
print(left+" "*(i-j)+"*"*(2*j+1))
print(" "*(n+addSpace) + "|")
print(" "*addSpace + "="*n + "V" + "="*n)
Related
I'm trying to recreate this shape (image below) using Python but I have some difficulties with the spaces.
This is the code that I have so far.
def shape(n):
for i in range(0,n):
for j in range(0,i+1):
print("*",end="")
print("")
for i in range(n,0,-1):
for j in range(0,i-1):
print("","*",end="")
print("")
shape(10)
I would appreciate it if you could help me.
def star(width = 10):
left_start = 0
right_start = 0
size = width
while right_start != size:
right_start += 1
print("*" * (right_start - left_start))
while left_start != right_start:
left_start += 1
print(" " * left_start + "*" * (right_start - left_start))
Hope this helps!
I want to print xmas tree with "stump" but i dont have any idea how to get that stump in there. I use Python language. My code looks like this so far:
def xmas(a):
stars = 1
for i in range(a):
print((' ' * (a - i)) + ('*' * stars))
stars += 2
if __name__ == "__main__":
xmas(3)
and it print this:
*
***
*****
And i want it to print like this:
*
***
*****
*
Do you have any tip how to get that stump over there?
Print the last line:
def xmas(a):
stars = 1
for i in range(a):
print((' ' * (a - i)) + ('*' * stars))
stars += 2
print(' ' * a + '*') # HERE
if __name__ == "__main__":
xmas(3)
Output
*
***
*****
*
I want to make a pattern as shown below:
I write this program which is given below. But I couldn't take ODD numbers. Could anybody please explain what the mistake is and what the solution is? Thanks.
*
def printPartten(n, k):
if (n < 0): # Base condition
return;
# Recursive call
printPartten(n - 1, k + 1);
for i in range(0, k): # it makes spaces
print(" ", end="");
for i in range(0, n): # for print *
print("* ", end = "");
print("\n", end=""); # for next line
def printPatternReverse(n, k):
if (n < 0): # Base condition
return;
for i in range(0, k): # it makes spaces
print(" ", end = "")
for i in range(0, n): # for print *
print("#", end = " ")
print("\n", end = "") # for next line
# Recursive calls
printPatternReverse(n - 1, k + 1);
# Call to printPartten function
n = int(input("Please enter the desire value for N: "))
printPartten(n, 0);
print("\n",end="")
printPatternReverse(n,0)
print("Thank You!!!")
*
My Output like this:
You example shows that it skips the even rows, so you could add a check for even rows using a modulus:
if (k % 2 == 0):
Applied to your code:
def printPartten(n, k):
if (n < 0): # Base condition
return;
# Recursive call
printPartten(n - 1, k + 1);
if (k % 2 == 0):
for i in range(0, k): # it makes spaces
print(" ", end="");
for i in range(0, n): # for print *
print("* ", end = "");
if (k > 0):
print("\n", end=""); # for next line
def printPatternReverse(n, k):
if (n < 0): # Base condition
return;
if (k % 2 == 0):
for i in range(0, k): # it makes spaces
print(" ", end = "")
for i in range(0, n): # for print *
print("#", end = " ")
print("\n", end=""); # for next line
# Recursive calls
printPatternReverse(n - 1, k + 1);
# Call to printPartten function
n = int(input("Please enter the desire value for N: "))
printPartten(n, 0);
print("\n",end="")
printPatternReverse(n,0)
print("Thank You!!!")
Output:
Please enter the desire value for N: 9
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
# # # # # # # # #
# # # # # # #
# # # # #
# # #
#
Thank You!!!
How to print text in stars box with loop
Example I want:
input1: input text
input2: high of stars
ex:
input1 = Your Name
input2 = 3
output:
***********
*Your Name*
***********
this my code i have now
def function(text):
"""start"""
name = len(text)
num = int(input())
for i in range(num):
for j in range(name + 2):
if i == 0 or j == 0 or i == num - 1:
print("*", end="")
print()
function(input())
output is
***********
*
***********
What should I do next.
You can multiply a string. "*" * 10 == '**********' - you don't need the inner for loop then.
Also, you need to print the thing as it should be, not make a border first - you can't just type onto what's already printed.
def function(text):
"""start"""
name_length = len(text) #renamed your variable, the name was confusing
num = int(input())
for i in range(num):
if i == 0 or i == num - 1: #removed inner loop
print("*" * (name_length+2))
else:
print("*{}*".format(text) #added printing the text and right border
function(input())
You didn't say what should happen for a higher number. I assumed the name should be repeated.
def function(text):
"""start"""
name = len(text)
num = int(input())
text_num = int((num - 1) / 2)
for i in range(num):
for j in range(name + 2):
if i == 0 or i == num - 1:
print("*", end="")
elif i == text_num:
if j == 0 or j == name + 1:
print("*", end="")
else:
print(text[j-1], end="")
print()
function(input())
Another option is to format by line as opposed to character.
Something like this could work:
def function(text):
"""start"""
name = len(text)
num = int(input())
for i in range(num):
if i == 0 or i == num - 1:
print('*' * (name + 2))
elif i == int(num / 2):
print('*' + text + '*')
else:
print('*' + ' ' * name + '*')
function(input())
With num = 3 outputs:
***********
*your name*
***********
With num = 5 outputs:
***********
* *
*your name*
* *
***********
You can build the box in a list and then substitute the name at the middle line:
name = "your name"
num = 3
box = ["*"+(" "*len(name))+"*"]*num
box[0] = box[-1] = "*" * (len(name)+2)
box[num//2] = f"*{name}*"
print("\n".join(box))
***********
*your name*
***********
for i in range(6):
for j in range(9):
if (i+j)%2 == 0:
_star = "*"
else:
_star = "*"
print(_star, end="")
print()
My output needs to be the left one: My output now is on the right:
***** **** *********
***** * * *********
***** * * *********
***** * * *********
***** **** *********
put your expected output on 2d area, and check out * and space is on what function.
def star_or_space(i, j):
# vertical line
if j == 5:
return ' '
# second box area
if 1 <= i <= 3 and 7 <= j <= 8:
return ' '
return '*'
for i in range(5):
for j in range(10):
_star = star_or_space(i, j)
print(_star, end="")
print()
You only have two patterns this "***** ****" OR this "***** * *"
at head and tail you are using this ("***** ****") pattern and in between you are using this ("***** * *") pattern. so below is the simplest logic for you.
top_bottom = "***** ****"
in_between = "***** * *"
rows = 5
for i in range(rows):
if i == 0 or i == 4:
print top_bottom
else:
print in_between