Question

In Python Trace "Stars.py" by drawing a table that shows the values of relevant variables (row...

In Python

Trace "Stars.py" by drawing a table that shows the values of relevant variables (row and star) and keeping track of the output. This program contains a nested for loop, so for each iteration of the outer loop, the inner loop executes in its entirety:

row star
1 1
2 1
2 2
3 1
3 2
3 3
... ...

#********************************************************************
# stars.py
#
# Demonstrates the use of nested for loops.
#********************************************************************

#-----------------------------------------------------------------
# Prints a triangle shape using asterisk (star) characters.
#-----------------------------------------------------------------

def main():
MAX_ROWS = 5

for row in range(1, MAX_ROWS + 1):
for star in range(1, row + 1):
print ('*', end = '')
  
print ('\n')

# Invoke/call main function.
main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution :

To easily implement a tabular format for tracing outputs, we use tabulate.py library for creating a table.

Steps to install tabulate.py:

1. Open your command prompt as an administrator.

2. Type "pip install tabulate"

3. It will take a few seconds to finish.

4. Exit command prompt.

Now the code:

from tabulate import tabulate       #To import tabulate function from tabulate library
columns = ["Row", "Stars"]          #Initializing a list containing the column names for the table
output_data = []                    # List to store row elements
def main():                         # main function as given in the question
    MAX_ROWS = 5
    for row in range(1, MAX_ROWS + 1):
        for star in range(1, row + 1):
            print('*', end='')
            output_data.append(tuple((row, star)))      #To append stars and row number in the list
        print("\n")

main()                                                  #Invoking of main function
print(tabulate(output_data, headers=columns, tablefmt="grid"))      #Printing in the table format, defining table format as grid

Screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
In Python Trace "Stars.py" by drawing a table that shows the values of relevant variables (row...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • use python and one loop to solve this problem Q6: Secondary Trace You are given a...

    use python and one loop to solve this problem Q6: Secondary Trace You are given a function that takes a square array, matrix, and returns the sum of the secondary diagonal values for matrix. This function employs a nested 2D for-loop. Write a function secondary trace single loop that solves the same problem using only one for-loop. For example, on the picture below secondary diagonal consists of values: a03, a12, a21, a30. a00 a01 a02 a03 a10 all a12 a13...

  • Java Submit a program in a .java file. The program should print a multiplication table for...

    Java Submit a program in a .java file. The program should print a multiplication table for the numbers 1-9. This requires a nested for loop. The outer loop moves from row to row, while the inner loop prints the row. Use tabs to separate the output, such as in the following statement:      System.out.println(“1\t2\t3\t4\t5\t6\t7\t8\t9”); Output should look similar to the following. 1      2     3     4     5     6     7     8     9 ------------------------------------------------------------------ 1       2     3     4     5     6        7     8     9...

  • [EC1-1] (patterns3.py) More fun with * patterns... Each of these is worth 0.25 points, except for...

    [EC1-1] (patterns3.py) More fun with * patterns... Each of these is worth 0.25 points, except for (f) and (g), which are worth 1 point each. Write a program that reads an int N >= 0, then prints out each of the following patterns of *. Here, you may use the * repetition operator, if you wish. Also, each pattern must be output via a single function call to the given named function with single parameter N. Examples for N==4 follow,...

  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer...

    -----------Python program------------------- Instructions: For this assignment, you will write complete a program that allows a customer to plan for retirement. Part 2: Generate a Retirement Planning Table: It's hard to decide how much you need to save for retirement. To help your customer visualize how long her nest egg will last, write a program that allows the user to generate a retirement planning table showing the number of months the savings will last for various combinations of starting account balance...

  • Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS...

    Python 2.7.14 Programming Assignment Shape Drawing With Notepad++(PLEASE READ AND FOLLOW THESE INSTRUCTIONS THOROUGLY AS THIS ASSIGNMENT IS FOR PYTHON 2.7.14. ONLY AND I REALLY NEED THIS TO WORK!!! ALSO PLEASE HAVE THE CODE PROPERLY INDENTED, WITH WHATEVER VARIABLES DEFINED, WHATEVER IT TAKES TO WORK FOR PYTHON 2.7.14. I feel like nothing I do is working and I'm trying everything I can think of. ): In this assignment, the student will create a Python script that implements a series of...

  • Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the...

    Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to display a warning if the number is less than 2 or greater than 10 and prompt the user to enter the data again until they enter a valid number Put a...

  • Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You...

    Python 3: Please follow the respective rubric for the following function definition. Rubric: Rectangle Shape You should implement the following functions to print a rectangle shape. • print_rectangle(length, width, fillChar, hollow). This function will use draw_shape_line() function to print a rectangle shape with the given length and width, If the hollow is true, the shape will be hollow rectangle, otherwise a filled rectangle. This function must not interact with the user. For example, the call print_rectangle(5, 10, '*', False) should...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2...

    E COMP 1224 Sec. 1 Spring 2017 Lab 1. I need the solution for question 2 only please. Thank you I need the solution for question 2 only please. Thank you COMP 1224 Sec. 1 Spring 2017 Lab 1 Your first lab will be similar to the examples discussed in class, covering programming hanction, and recursion. It includes the following tasks Using CdC write loop(s) to control and output a star pattern as follows: giving a integer N (use cin...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT