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
*
***
*****
*
Related
It seems that my decoration sometimes appears just outside the triangle... How can I fix this?
import random
n = int(input('enter n: '))
x = 1
for i in range(n):
if i == 0:
count = 0
else:
count = random.randint(0, x)
print(' ' * (n - i), '*' * count, end = '')
if i == 0:
print('&', end = '')
else:
print('o', end = '')
print('*' * (x - count - 1))
x += 2
What do I get for value n = 10:
&
***o
*o***
o******
******o**
*********o*
*************o
*****o*********
*************o***
******************o
The random number you generate may return x as randint includes the second value in the set of possible values. So pass x - 1 to randint:
count = random.randint(0, x - 1)
It is also a pity that you have a check for i == 0 in your loop. In that case, why not deal with that first case outside of the loop, and start the loop at 1? It will make your code a bit more elegant.
def tree(n):
from random import sample
body = ["&"] + ["".join(sample("o" + "*" * (k:=x*2+1), k=k)) for x in range(1, n)]
spaces = [" " * (x-2) for x in range(n, 1, -1)]
return "\n".join("".join(tpl) for tpl in zip(spaces, body))
print(tree(10))
Output:
&
o**
****o
o******
*******o*
********o**
*******o*****
**************o
o****************
I love christmas tress! This is my take on the question :-)
import random
n = int(input('Enter tree height: '))
print(" " * (n-1) + "&")
for i in range(2, n+1):
decoration = random.randint(1, (i-1)*2 -1)
print(" " * (n - i) + "*" * decoration + "o" + "*" * ((i-1)*2 - decoration))
Enter tree height: 12
&
*o*
**o**
*o*****
*o*******
********o**
*******o*****
***********o***
********o********
****************o**
***o*****************
*********o*************
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)
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
I'm working on this problem, that reads as follows:
Problem: Write a function that draws a pyramid on your screen. The function needs two arguments. The first is the height of the pyramid. The second argument is optional: if not supplied, the symbol "#" should be used to draw the pyramid. Otherwise, if the users enters " * " for example, the pyramid should consist of asterisks.
Attempt: I wrote this program:
def main():
h = int(input("Please enter the height of the pyramid: "))
symbol = str(input("Enter the desired symbol or press enter for standard (#): "))
def pyramid(h,symbol):
if symbol == "" or symbol == "#":
for i in range(h):
pyr = print(" " * (h-i - 1) + "#" * (2 * i + 1))
return pyr
else:
for i in range(h):
pyr = print(" " * (h-i - 1) + symbol * (2 * i + 1))
return pyr
print()
main()
But this is not working properly when I try to call this. Can someone point out my mistakes? Also, I'm not sure how to deal with the 'optional' property of the function. Should I stick to my approach or is there a better way to define that ?
your logic is fine, you can try
def pyramid(h,symbol):
if symbol == "" or symbol == "#":
for i in range(h):
print(" " * (h-i - 1) + "#" * (2 * i + 1))
else:
for i in range(h):
print(" " * (h-i - 1) + symbol * (2 * i + 1))
print()
pyramid(5, "#")
#
###
#####
#######
#########
your problems:
pyr = print(" " * (h-i - 1) + "#" * (2 * i + 1)) .... print function return None, then pyr store None
return pyr statement return pyr variable content and finish pyramid function
pyramid function isn't calling never
Improving code
you can remove unnecessary if,
def pyramid(h,symbol="#"):
for i in range(h):
print(" " * (h-i - 1) + symbol * (2 * i + 1))
print()
pyramid(5)
you can return a str
def pyramid(h,symbol="#"):
out = ""
for i in range(h):
out += (" " * (h-i - 1)) + (symbol * (2 * i + 1)) + "\n"
return out
print(pyramid(5))
or, online solution
def pyramid(h,symbol="#"):
return "\n".join((" " * (h-i - 1)) + (symbol * (2 * i + 1)) for i in range(h))
print(pyramid(5))