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
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 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...
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;
Write a python code that will ask the user to enter an integer number n. Construct...
Write a python code that will ask the user to enter an integer number n. Construct a recursive function that prints numbers 1 to n in the form “11223344..nn”.
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
When the user enters 2 the following program segment should print the message “You’ve selected to...
When the user enters 2 the following program segment should print the message “You’ve selected to get an A”, however it always prints “You’ve selected to get a C”. Rewrite the code segment to behave properly, i.e., print the proper message: int selection; cout << “Please enter the grade you would like to receive “ << endl; cout << “1 = C, 2 = A”; cin >> selection; if (selection = 1) cout << “You’ve selected to get a C“...
This phyton program requires you to accept from the user a three-digit integer (100-999), and prints...
This phyton program requires you to accept from the user a three-digit integer (100-999), and prints the reverse of that integer. You can assume that the input is an integer, but should not assume it's in the right range. If it's not in the right range, print "Illegal input:" and the number. Here are three sample runs: Enter a three digit decimal integer: 234 The number in reverse is: 432 Enter a three digit decimal integer: 1345 Illegal input: 1345...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER. printDouble should accept one value and should print the value of that number times 2. printEndMessage should not accept any values and should print a message indicating that the program has finished. def main(): num1 = int(input("Please enter your number ")) printDouble(num1) printEndMessage() main()
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT