Question

i need this code to print a number of stars depending on how much the user...

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()

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
C - Language Write an expression that executes the loop while the user enters a number...
C - Language Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of -1. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds,...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
Python 3 Conversion and exception handling Create a function that asks the user to enter a...
Python 3 Conversion and exception handling Create a function that asks the user to enter a number and returns the number. * function name: get_int * parameters: prompt (string) * returns: int * operation: Use the "prompt" parameter as the parameter to an input() call in order to ask the user for a number. Convert the number to a int using the int() function and return this number from the function. but - If the user enters something that cannot...
So I am trying to write a factor generator. User provides an integer and program needs...
So I am trying to write a factor generator. User provides an integer and program needs to return all factors. I have found an example the code that does this function, but I can't understand how it does that. Please explain line by line how this works.     System.out.print("Enter an integer: ");        int number = input.nextInt();        int index = 2; //Prompts user for an integer and assigns it to variable number, and integer index is declared...
In C++, create an input validation where the user enters a signed whole number and repeats...
In C++, create an input validation where the user enters a signed whole number and repeats this process until the user enters a zero. Do not accept any value with a decimal fraction or other extraneous characters, including dollar signs. The number's sign must be preserved. Some of the steps has been done for you. Fill out the program in C++. See below for the expected console output. Program: #include <iostream> using namespace std; int main(){    signed int input;...
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
Description: The purpose of this assignment is to practice writing code that calls functions, and contains...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains loops and branches. You will create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. General Comments: Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement an...