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
  • CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program...

    CAN YOU PLEASE DO THIS IN JAVA! WILL LEAVE GOOD RATING THANK YOU Modify your program that reads grades from the user, so that it has a method that checks if a particular input is valid i.e. as long as the user types invalid input, the user should be given another chance to enter input (it would also be good to let the user know that their input is invalid). Moreover, only a valid grade should be used in computing...

  • 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...

  • 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...

  • 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...

  • [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,...

  • I need help for the order of growth for functions in Python 3. Q1: What is...

    I need help for the order of growth for functions in Python 3. Q1: What is the order of growth for the following functions? Kinds of Growth Here are some common orders of growth, ranked from no growth to fastest growth: 1. Θ(1) — constant time takes the same amount of time regardless of input size 2. Θ(log n) — logarithmic time 3. Θ(n) — linear time 4. Θ(n log n) — linearithmic time 5. Θ(n2 ) 6. Θ(n3 ),...

  • For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...

    For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...

  • Can someone finish this class for me? There are comment instructions with what to do in...

    Can someone finish this class for me? There are comment instructions with what to do in the methods already. public class ArrayDemo   {                               public void demonstrateTask1(){                                System.out.println("Demonstrate   Task   1");                                                               int[]   numbers =   null;   //   note   that   numbers   references   nothing   (null)   initially                           ...

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