Python Nested Loop Patterns - python

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

Related

Print xmas tree

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
*
***
*****
*

how to create Christmas tree in python with layer and base

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)

Python STAR & HASH ODD DIAMOND print with recursion

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

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*
***********

How to make this code more efficient and functional?

I have a code that is ridiculously long and I need to use it in many methods. I think there's definitely a better way to write it. But I'm struggling to find it. What the code does is that it:
Takes 12 numbers
It inputs them in a list(let us call it count)
Creates 12 stars variables
It needs to create some sort of matrix that looks like this:
******* count1
******** count2
********** maxcount (could be any count from count1 to 12, as long as it is the maximum
***** count5
etc..
So to explain that, we have 12 count variables, I needed to take the largest variable of them, so I put them in a list. After putting them in a list, I selected the max(count) in order to build my astericks representation.
The maximum number of astericks possible is 10. THerefore, the maximum amongst the 12 counts should have 10 stars next to it, and all the others will have their stars relatively.
This is my code, but it doesn't seem optimal at all to me, lots of variables created and initialized as well as it takes some time.
count = list()
count.append(count1)
count.append(count2)
count.append(count3)
count.append(count4)
count.append(count5)
count.append(count6)
count.append(count7)
count.append(count8)
count.append(count9)
count.append(count10)
count.append(count11)
count.append(count12)
stars1 = 0
stars2 = 0
stars3 = 0
stars4 = 0
stars5 = 0
stars6 = 0
stars7 = 0
stars8 = 0
stars9 = 0
stars10 = 0
stars11 = 0
stars12 = 0
stars1 = int((count1 * 10) / max(count))
stars2 = int((count2 * 10) / max(count))
stars3 = int(count3 * 10 / max(count))
stars4 = int(count4 * 10 / max(count))
stars5 = int(count5 * 10 / max(count))
stars6 = int(count6 * 10 / max(count))
stars7 = int(count7 * 10 / max(count))
stars8 = int(count8 * 10 / max(count))
stars9 = int(count9 * 10 / max(count))
stars10 = int(count10 * 10 / max(count))
stars11 = int(count11 * 10 / max(count))
stars12 = int(count12 * 10 / max(count))
astericks1 = ""
astericks2 = ""
astericks3 = ""
astericks4 = ""
astericks5 = ""
astericks6 = ""
astericks7 = ""
astericks8 = ""
astericks9 = ""
astericks10 = ""
astericks11 = ""
astericks12 = ""
for i in range(1, 11):
if (i <= stars1):
astericks1 += "*"
else:
astericks1 += " "
if (i <= stars2):
astericks2 += "*"
astericks2 += " "
if (i <= stars3):
astericks3 += "*"
else:
astericks3 += " "
if (i <= stars4):
astericks4 += "*"
else:
astericks4 += " "
if (i <= stars5):
astericks5 += "*"
else:
astericks5 += " "
if (i <= stars6):
astericks6 += "*"
else:
astericks6 += " "
if (i <= stars7):
astericks7 += "*"
else:
astericks7 += " "
if (i <= stars8):
astericks8 += "*"
else:
astericks8 += " "
if (i <= stars9):
astericks9 += "*"
else:
astericks9 += " "
if (i <= stars10):
astericks10 += "*"
else:
astericks10 += " "
if (i <= stars11):
astericks11 += "*"
else:
astericks11 += " "
if (i <= stars12):
astericks12 += "*"
else:
astericks12 += " "
First of all this kind of if,elif statements and variable creations are generally bad coding practice. Secondly, as a little advice you should always think that I can create same functionality with the list instead of two different variables for the same reason (count1,count2, etc.).
As I understand from your code you print maximum 10 stars for max value. So the code below should produce same behavior with yours using list comprehensions. Also I should mention that it can be written more efficiently but I wanted to be clear and simple for you.
variable_num = 12
count = [int(input()) for i in range(variable_num)]
max_num = max(count)
count = [(i*10)//max_num for i in count]
for ind,i in enumerate(count):
print("{} count {}".format(int(i)*"*",ind))
As simple as:
# Get 12 numbers as input
s = input("Input 12 numbers separated by a white space")
# Save them in a list and calculate the maximum
nums = [int(item) for item in s.split()]
max_num = max(nums)
for idx, item in enumerate(nums):
# Calcuate number of stars, ensuring number of stars are never more then 10
num_stars = int(item * 10 / max_num)
# print the stars
print('*' * num_stars, 'count{}'.format(idx))
Sample output will be
Input 12 numbers separated by a white space1 2 3 4 5 6 1 2 3 4 5 6
* count0
*** count1
***** count2
****** count3
******** count4
********** count5
* count6
*** count7
***** count8
****** count9
******** count10
********** count11

Categories

Resources