pythonfor-loop

Astrologers' star pattern in Python


Unable to print star pattern in python using Jupyter notebook. May be the issue with Jupyter Notebook or else. Please help and highlight the mistake.

print("Astrologer's star")
print("How many row of star you want?")
n = int(input())
print("Please input 1 if you want stars in ascending form or \nprint 0 if you want stars in descending form")
oneORzero = bool(int(input()))

if oneORzero == True:
    for i in range(1, n+1):
        for j in range(1, i+1):
            print("*")
    print("")
    
else:
    for i in range(n, 0, -1):
        for j in range(1, i+1):
            print("*")
    print("")

This is the image which system was showing.


Solution

  • print() prints a whole line every time it is called, unless specified otherwise. You can use the parameter end="" to clarify you don't want it to add a new line.

    You would then also need to print a newline character at the end of the inner for-loop. This could look like the following:

    for i in range(n, 0, -1):
        for j in range(1, i+1):
            print("*", end="")
        print("")
    

    Notice, that print("") is indented one more level.