How can I change this code that instead of printing the shape below, it prints it like a lozenge (rhomboid)?
def star (m):
for i in range (m + 1):
print ("*" * i)
for i in range ( m + 1 ):
print ("*" * (m - 1))
m -= 1
Output
*
**
***
****
***
**
*
A lozenge is equilateral, a rhomboid typically not. For illustration, I will do a rhomboid, but allow it to be equilateral.
To define a function, you should first specify the domain, in this case positive ints (or include 0 if you wish).
Functions should generally return an object that can be compared with the desired output for a given input. You can then write an automated test for example outputs. In this case, the function should produce an iterable of strings. The desired output can most easily be represented as a tuple or list of strings and the function output converted if necessary.
r43 = (
'****',
' ****',
' ****',
)
def rhom(width, height):
if not(isinstance(width, int) and width > 0 and
isinstance(height, int) and height > 0):
raise ValueError('width and height must be positive ints')
stars = width * '*'
for i in range(height):
yield i * ' ' + stars
out43 = tuple(rhom(4, 3))
print(r43 == out43)
for line in out43:
print(line)
prints
True
****
****
****
I like the following a bit better because it's more symmetrical:
def star(m):
def star_line(n):
print('{s:^{f:d}s}'.format(s='*'*n, f=m))
for i in range(1,m,2):
star_line(i)
for i in range(m,0,-2):
star_line(i)
star(5)
*
***
*****
***
*
This solution uses the nice feature of Python's new(er) formatting syntax which enables you to specify the width of a field dynamically as well as the contents of the field: e.g. '{s:^{f:d}}'.format(s='*'*3, f=5) becomes '{s:^5s}'.format(s='***') which becomes ' *** '.
def print_lozenge(num):
str = '*'
str_2 = str + (num - 1) * ' *'
size = 2 * num - 1
for n in range(num):
print str.center(size, ' ')
str += ' *'
for n in range(num):
str_2 = str_2[: -2]
print str_2.center(size, ' ')
print_lozenge(5)
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Is this what you wanted?
def star(m):
for i in range(1, m + 1):
print(("*" * i).center(m))
for i in reversed(range(1, m)):
print(("*" * i).center(m))
star(10)
Output:
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
Related
Write a recursive function called draw_triangle() that outputs lines of *'s to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the first line for correct formatting.
Hint: The number of '*' increases by 2 for every line drawn.
Ex: If the input of the program is: 3,
Then the function draw_triangle outputs:
*
***
If the input of the program is 19,
Then the function outputs
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
No space is output before the first asterisk on the last line when the base length is 19.
I was able to code this non-recursively as follows:
def draw_triangle(n):
lines_to_print = int(((n // 2) + 1))
spaces_to_print = 9
asts_to_print = 1
for i in range(lines_to_print):
print(spaces_to_print * ' ', end='')
print(asts_to_print * '*', end='')
print()
spaces_to_print -= 1
asts_to_print += 2
base_length = int(input())
draw_triangle(base_length)
But I cannot, for my life, figure out how to do it recursively, let alone with only one argument. What am I missing about recursion?
The simple trick here is to use the str.center() method.
def draw_triangle(n):
if n == 1:
print('*'.center(19))
else:
draw_triangle(n-2)
print((n*'*').center(19))
Test it:
draw_triangle(19)
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
One method is is to define functions to print the symbole you want to use and the spaces between them:
def print_ecart(ecart):
if (ecart == 0):
return;
print(" ", end = "")
print_ecart(ecart - 1)
def print_etoile(etoile):
if(etoile == 0):
return
print("* ", end = "")
print_etoile(etoile - 1)
def pattern(n, num):
if (n == 0):
return
print_ecart(n - 1)
print_etoile(num - n + 1)
print("");
pattern(n - 1, num)
In this case print_ecrat prints spaces and print_etoile prints the stars:
pattern(10,10)
returns:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
By printing after the recursion itself happens, you can print smaller lines over the bigger lines:
def draw_triangle(n):
spaces = 9 - n // 2 # Ensures correct spacing for each line
if n == 1:
print(spaces * ' ' + '*')
else:
draw_triangle(n-2)
print(spaces * ' ' + n * '*')
draw_triangle(19)
Well, it is a quite interesting question.
The main obstacle is that you have to preserve initial base length of triangle.
You can't save it via assigning input value to another parameter inside a recursive function because:
only one argument is allowed
assigning will work every function call.
Not sure if the following code is convenient with the conditions of the task but it may be useful to illustrate an idea of recursive functions for you:
base_length = 20
def draw_triangle(base=base_length):
if base % 2 == 0:
base = base - 1
draw_triangle(base)
else:
if base > 0:
# print(base)
# print(int(base/2 - 1))
print(' '*int((base - 2)/2+1) + '*'*(base_length - base) + ' '*int((base - 2)/2))
base = base - 2
draw_triangle(base)
elif base == 0:
return 0
draw_triangle(base=base_length)
Formally there is only one argument in this recursive function, but you have to define base_length paramater outside of the function and the function uses base_length inside.
So I have to print * in the shape of A which I did.
pattern = ""
for row in range(7):
for col in range(5):
if ((col==0 or col==4) and row!=0) or ((row==0 or row==3) and (col>0 and col<4)):
pattern = pattern + "*"
else:
pattern = pattern + " "
pattern = pattern + "\n"
pattern = pattern + " "
print(pattern, end="")
OUTPUT:
***
* *
* *
*****
* *
* *
* *
But I want to print more A horizontally.
Example: L displayed as star program 3 times looks as below
* * *
* * *
* * * * * * * * *
Please help!
I did not change a lot in your code, but thought that putting it in a function might be useful. As you can see in the last line, you now just have to call it with the number of letters you want to print. Furthermore, I added a for-loop in between yours, in order to get all the required symbols into the according line.
Lastly, I added an if/else staement that checks this is the end of the row and otherwise does not go into the next line.
def multiplePatterns(n):
pattern = ""
for row in range(7):
for i in range(n):
for col in range(5):
if ((col==0 or col==4) and row!=0) or ((row==0 or row==3) and (col>0 and col<4)):
pattern = pattern + "*"
else:
pattern = pattern + " "
if i == n-1:
pattern = pattern + "\n"
else:
pattern += " "
pattern = pattern + " "
return pattern
print(multiplePatterns(2))
The easiest way is to use a library like pyfiglet : https://pypi.org/project/pyfiglet/ .
If you want to do it yourself, python console write per line, you must print your text (ex:"AAA") line per line.
if your final goal is just that, then:
print(" *** *** ***")
print("* * * * * *")
print("* * * * * *")
print("***** ***** *****")
print("* * * * * *")
print("* * * * * *")
print("* * * * * *")
will do the job.
If your goal is to write any text, then its probably better to create some kind of "font" with each letter in a list.
I'm supposed to create a recursive statement that if first calls triangle(n) it returns
'******\n *****\n ****\n ***\n **\n *'
This above is called for triangle(6) and if I print(triangle(6)) it returns below.
******
*****
****
***
**
*
Then I must create another code recursive_triangle(x, n) that returns a string with the LAST x lines of a right triangle of base and height n. For example if I did recursive_triangle(3, 6) it returns
' ***\n **\n *'
and if i print it should returns
***
**
*
So far my code is
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if n == 1:
return "*"
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1)
for print(triangle(4)) this is what i got
****
***
**
*
How do I modify the code to get the output above?
Your recursion case is ill-formed:
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1)
First of all, this code returns after a single iteration: return ends your function instance, so i never gets to a value of 2. You need to do something simple, and then recur on a simpler case to handle the rest.
Next, triangle exists only to call recursive_triangle. Then, recursive_triangle needs to call itself, not loop back to triangle.
Finally, note that recursive_triangle utterly ignores the parameter k. This value is critical to determine where in the line to place the asterisks.
Each instance of recursive_triangle should produce a single line of the triangle -- you have that correct -- and then concatenate that line with the remainder of the triangle, returning that concatenated whole to the instance that called it. You'll want something roughly like:
else:
line = ... # build a line of k-n spaces and n asterisks
return line + recursive_triangle(k, n-1)
Can you take it from there? Among other things, remember to insert a few useful print commands to trace your execution flow and the values you generate.
First of all, there are better ways to achieve this.
However, if you really want to go this way, the following code can fix the spacing issues.
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if n == 1:
return "*"
else:
for i in range(1, n+1):
return ("*" *n) + "\n" + (' ' * i) + triangle (n - 1).replace("\n", "\n ")
which gives you
****
***
**
*
in Python 3.6.5.
You can count the row with r and use a secondary parameter to count the spaces, s
def triangle (r = 0, s = 0):
if r is 0:
return ""
else:
return (" " * s) + ("*" * r) + "\n" + triangle (r - 1, s + 1)
print (triangle(5))
# *****
# ****
# ***
# **
# *
code.py:
#!/usr/bin/env python3
import sys
#### DO NOT modify the triangle(n) function in any way!
def triangle(n):
return recursive_triangle(n, n)
###################
def recursive_triangle(k, n=0):
'''
Takes two integers k and n
>>> recursive_triangle(2,4)
' **\\n *'
>>> print(recursive_triangle(2,4))
**
*
>>> triangle(4)
'****\\n ***\\n **\\n *'
>>> print(triangle(4))
****
***
**
*
'''
# --- YOUR CODE STARTS HERE
if k == 0:
return ""
else:
return "\n".join(["".join([" " * (n - k), "*" * k]), recursive_triangle(k - 1, n)])
#return " " * (n - k) + "*" * k + "\n" + recursive_triangle(k - 1, n)
def main():
print("triangle(6):\n{:s}".format(triangle(6)))
print("recursive_triangle(3, 6):\n{:s}".format(recursive_triangle(3, 6)))
print("repr recursive_triangle(2, 4): {:s}".format(repr(recursive_triangle(2, 4))))
print("repr triangle(4): {:s}".format(repr(triangle(4))))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
Notes:
To make things simpler, you can look at recursive_triangle(k, n)'s argument meanings like:
k: Recursion step, and also the number of "*" characters. Decrements with every recursive function call
n: 1st (longest) triangle line length (the number of SPACEs + k). Remains constant inside recursion
Current line contains n - k SPACEs followed by k "*" s (n characters in total). Lines are joined together via [Python 3]: str.join(iterable) (or "manually", in the (next) commented line)
The function calls itself until there are no more "*" characters (k becomes 0)
Output:
(py35x64_test) e:\Work\Dev\StackOverflow\q052652407>"e:\Work\Dev\VEnvs\py35x64_test\Scripts\python.exe" code.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
triangle(6):
******
*****
****
***
**
*
recursive_triangle(3, 6):
***
**
*
repr recursive_triangle(2, 4): ' **\n *\n'
repr triangle(4): '****\n ***\n **\n *\n'
I am trying to print out an arrow head using *s.
So far my code looks like this.
def head(n):
while n > 0:
print n * "*"
n = n - 1
print head(input())
and it works but if for example I enter 11, it prints this:
***********
**********
*********
********
*******
******
*****
****
***
**
*
But I want it to print like this:
*
***
*****
*******
*********
***********
Which has less arrows, but I can't figure out how to do it.
It makes the function a little simpler to think in terms of how many lines do you want:
def head(lines):
for n in range(1,lines*2,2): # count 1,3,5...
print(('*'*n).center(lines*2-1))
Output:
>>> head(5)
*
***
*****
*******
*********
Here's an alternate way to use a variable length format that is a little less obvious:
def head(lines):
for n in range(1,lines*2,2):
print('{:^{}}'.format('*'*n,lines*2-1))
use string formatting:
def head(size):
n=1
while n < size+1:
stars = n * "*"
print '{:^30}'.format(stars)
n += 2
it will center your asterisks on the field 30 chars wide.
def printHead(n):
for l in range(1,n):
print " "*(n-l)+"*"*(1 if l==1 else 2*l-1)
Each row has level-1 spaces. Then if it's the first level one start, otherwise it has 2*level-1.
>>> printHead(6)
*
***
*****
*******
*********
def head(n):
total = 2 * n - 1
s = ''
for i in xrange(1, n + 1):
k = 2 * i - 1
s += ' ' * ((total - k) / 2) + '*' * k + '\n'
return s
The number of stars on a line is equal to 2n - 1 where n is the line number.
I wouldn't call the parameter "n" in this function as it is not clear to me whether it refers to lines or stars.
You can use the center function to surround a string with whitespace, based on a specified width. You want the arrow to be centred around the longest line so you need 2 variables, one to keep track of the current line and another to remember the largest line.
You want to iterate in the opposite direction to what you have demonstrated, as you want the arrow to point up, not down.
I think this should work for you:
def head(total_lines):
for current_line in range(1, total_lines + 1):
print ((2 * current_line - 1) * "*").center(2 * total_lines - 1)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
ASCII Python Art # 2
i = 10
while i>0:
print i*'*' + 2*(10-i)*' ' + i*'*'
i -=1
for x in range(1,11):
print x* '*' + 2*(10-x)*' '+ x*'*'
x +=1
I am trying to make a asterisk diamond with 19 lines.
I get 20 instead.
This is what I want:
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********************
How can I get rid of the doubling up in height in the middle of the asterix.
cheers.
The basic idea is to decide which loop should print the line with just a single start on each side. Then modify the other loop not to print that line (i.e. end or start with the 2-star line).
Change
for x in range(1,11):
to
for x in range(2,11):
Also, you can get rid of the very last line, as the second loop does all the incrementing of x on its own.
For readabilities sake I'd also switch to just a single kind of loop.
For example you can write the first loop using (notice: this loop will not run the x=1 case, so you'd have to use the unmodified version of your second loop).
for x in range(10, 1, -1):
print x* '*' + 2*(10-x)*' '+ x*'*'
You've already worked out how to print each line.
Now the last part is to generate the correct sequence of number to drive the line printing.
l = range(10,1,-1)
l.extend(range(1,11))
for x in l:
print x*'*' + 2*(10-x)*' ' + x*'*'
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********************
I had some difficulty to find the trick:
def diamond(ni):
li = [ i*'*' + (2*(ni-i) - 1)*' ' + (i - i//ni)*'*'
for i in xrange(ni,0,-1)]
li.extend(i*'*' + (2*(ni-i) - 1)*' ' + (i - i//ni)*'*'
for i in range(2,ni+1))
return '\n'.join(li)
print diamond(7)
draws
*************
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
*************
Noticing that
li.extend(i*'*' + (2*(ni-i) - 1)*' ' + (i - i//ni)*'*'
for i in range(2,ni+1))
does the same (less one line) than
li = [ i*'*' + (2*(ni-i) - 1)*' ' + (i - i//ni)*'*'
for i in xrange(ni,0,-1)]
but in reverse order, we can simplify:
def symetric_diamond(ni):
li = [i*'*' + (2*(ni-i) - 1)*' ' + (i - i//ni)*'*'
for i in xrange(ni,0,-1)]
li.extend(li[-2::-1])
return '\n'.join(li)
Please, note that
print '\n'.join(str(i) for i in xrange(500))
is displayed instantly, because the program computes the string '\n'.join(str(i) for i in xrange(500)) before to print it in one shot, while
for i in xrange(500):
print str(i)
is far longer to be displayed, because the computer prints 500 strings one after the other, and each call to print is long
.
There's another manner to print a diamond,I will write it now.
Plus
def format_diamond(nl):
ni = (nl+1)/2
li = ['{:{fill}{align}{width}}'.format((2*(ni-x) - 1)*' ',fill='*',align='^',width=2*ni-1)
for x in xrange(ni,0,-1)]
li.extend(li[-2::-1])
return '\n'.join(li)
-> all these functions give a diamond of nl-1 lines when nl is even.
Edit
Finally, what I prefer is:
def symetric_diamond(nl):
'''Returns a diamond of nl lines if nl is odd,
and nl-1 lines if nl is even'''
ni = (nl+1)//2
li = [ (2*ni-1) * '*' ]
li.extend(i*'*' + (2*(ni-i) - 1)*' ' +i*'*' for i in xrange(ni-1,0,-1))
li += reversed(li[0:-1]) # in-place extension
return '\n'.join(li)
because of the in-place extensions (with extend and reversed) and the absence of the horrid (i - i//ni)*'*'
x = list(range(0, 20, 2))
x += reversed(x[:-1])
for _ in x:
stars, spaces = "*" * ((20 - _) / 2), " " * _
print stars + spaces + stars
As you are doubling code, you could rewrite your program to
def line(x): print x* '*' + 2*(10-x)*' '+ x*'*'
i = 10
while i>0:
line(i)
i -=1
for x in range(1,11):
line(x)
# x +=1 # unnecessary - happens already due to the for loop
And now, you can make one loop out of the two (removing the function again):
rg = list(range(10, 1, -1)) + list(range(1, 11))
for x in rg:
print x* '*' + 2*(10-x)*' '+ x*'*'