i need this code to print a number of stars depending on how much the user wants and after * it prints no stars. if the user enters a negative number it should print "error invalid number"
this is my code so far:
def stars(n,i): stars(n, 1) if n <= 0: return "No stars" if i <= n: print("* ", end="") stars(n, i + 1) else: print("no stars") stars(n - 1, 1) n = int(input("enter an integer")) def main(): stars() number = n def stars(n, i): if n < 1: return if i <= n: print("* ", end="") stars(n, i + 1) else: print("") stars(n - 1, 1) n = int(input("enter an integer: ")) stars(n, 1) main()
In case of any queries,please comment. I would be very happy to
assist all your queries.Please give a Thumps up if you like the
answer.
Program
def stars(n):
if n <0:
print( "Error!!! Invalid number.")
return
else:
for i in range(n + 1, 0, -1):
for j in range(0, i - 1):
print("*",end='')
print(end=' ')
print("no stars")
def main():
n = int(input("Enter an integer: "))
stars(n)
main()
Output
Enter an integer: 5
***** **** *** ** * no stars
>>>
Enter an integer: 7
******* ****** ***** **** *** ** * no stars
>>>
Enter an integer: -8
Error!!! Invalid number.
Program Screenshot
Get Answers For Free
Most questions answered within 1 hours.