I just started Python 2.7 very recently, but I\'m stuck at this a problem:
Make a program that prints an \"M\" pattern of asterisks. The user shall in
Sometimes it helps to sketch out a table of the values you need to generate:
h notch_rows no_notch_rows row "*"s " "s "*"s
3 1 1 1 1 1
2 1 3
2 3
5 2 1 1 3 1
2 2 1 2
3 1 5
2 5
3 5
7 3 1 1 5 1
2 2 3 2
3 3 1 3
4 1 7
2 7
3 7
4 7
This results in an implementation like
def mstar(h, star_ch="*", space_ch=" "):
assert h % 2, "h must be odd"
notch_rows = h // 2
no_notch_rows = notch_rows + 1
rows = []
for row in range(1, notch_rows + 1):
stars = star_ch * row
spaces = space_ch * (h - 2 * row)
rows.append(stars + spaces + stars)
for row in range(1, no_notch_rows + 1):
stars = star_ch * h
rows.append(stars)
return "\n".join(rows)
def main():
h = int(input("Height of M-Star? ")) # use raw_input for Python 2.x
print(mstar(h))
if __name__ == "__main__":
main()
and runs like
Height of M-Star? 5
* *
** **
*****
*****
*****
Height of M-Star? 9
* *
** **
*** ***
**** ****
*********
*********
*********
*********
*********