140k views
2 votes
Assume the availability of a function called printStars. The function receives an integer value as an argument. If the argument is positive, the function prints (to standard output) the given number of asterisks. Thus, if printStars(8) is called, ******** (8 asterisks) will be printed.Assume further that the variable starCount has been declared and initialized to contain a positive integer value.Write some code that prints starCount asterisks to standard output by:first printing a single asterisk and no other characters then calls printStars to print the remaining asterisks.Hint: Make sure to use the "end" option for print characters without the default newline, e.g. print("hello", end="")

2 Answers

4 votes

Final answer:

To print starCount asterisks, you can start by printing a single asterisk using the print() function. Then, you can call the printStars() function with the remaining number of asterisks to print.

Step-by-step explanation:

To print starCount asterisks, you can start by printing a single asterisk using the print() function. Then, you can call the printStars() function with the remaining number of asterisks to print.

starCount = 8
print('*', end='')
printStars(starCount-1)

The printStars() function can be defined as follows:

def printStars(count):
if count > 0:
print('*', end='')
printStars(count-1)
nting asterisks in Python here:
User Dh YB
by
4.8k points
6 votes

Answer:

The solution code is written in Python 3.

  1. def printStars(count):
  2. for i in range(0, count):
  3. print("*", end="")
  4. starCount = 8
  5. print("*", end="")
  6. printStars(starCount-1)

Step-by-step explanation:

Firstly, create a function printStars() that take one integer value as argument. (Line 1 -3)

  • To print the asterisks based on the input integer, count, we can use the for-loop and Python in-built method range() (Line 2-3)
  • The range() method will accept two arguments, start index and stop index, which will control the number of iterations the for-loop will go through. If the count = 5, the for-loop will traverse through the range of number from 0, 1, 2, 3, 4 (A total of 5 rounds of iterations).

Once the function printStars() is ready, create a variable starCount and initialize it with 8 (Line 5).

Print the first asterisk (Line 6) and then followed with calling the function printStars() to print the remaining asterisk (Line 7). The argument that passed to the printStarts() function is starCount-1 as the first asterisk has been printed in Line (6).

User Tim Swast
by
5.8k points