This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The arrow dimensions are defined by user specified arrow base height, arrow base width, and arrow head width.
(1) Modify the given program to use a loop to output an arrow base of height arrow_base_height.
(2) Modify the given program to use a loop to output an arrow base of width arrow_base_width.
(3) Modify the given program to use a loop to output an arrow head of width arrow_head_width.
(4) Modify the given program to only accept an arrow head width that is larger than the arrow base width.
Use a loop to continue prompting the user for an arrow head width until the value is larger than the arrow base width.
while arrow_head_width <= arrow_base_width: arrow_head_width = int(input('Enter arrow head width:\n')) Example output for arrow_base_height = 5, arrow_base_width = 2, and arrow_head_width = 4: Enter arrow base height: 5 Enter arrow base width: 2 Enter arrow head width: 4 tot tr

Answer :

mahamnasir

Answer:

Here is the Python program:

arrow_base_height = int(input('Enter arrow base height: \n'))  #prompts user to enter the arrow base height

arrow_base_width = int(input('Enter arrow base width: \n')) #prompts user to enter the arrow base width

arrow_head_width = int(input('Enter arrow head width: \n'))  #prompts user to enter the arrow head width

while (arrow_head_width <= arrow_base_width):  #ensures that the arrow head width is greater than base width

    arrow_head_width = int(input('Enter arrow head width: \n'))  #keeps prompting user to enter arrow head width until the value is larger than the arrow base width.

for i in range(arrow_base_height):  #to print arrow shaft

   for j in range(arrow_base_width):  #iterates through arrow base width

       print ('*', end='')  #prints asterisks

   print ()  #prints new line

for i in range(arrow_head_width):  #iterates through arrow head width arrow head

   for j in range(arrow_head_width-i):  #iterates through arrow head width-i

        print ('*', end='')  #prints asterisks

   print()  #prints new line

       

Explanation:    

The program works as follows:

Suppose user enters 5 as arrow base height, 2 as arrow base width and 4 as arrow head widths so,

arrow_base_height = 5

arrow_base_width = 2

arrow_head_width = 4

Since the arrow_head_width is not less than arrow_base_width so the while loop at the start does not execute. Program control moves to the statement:

for i in range(arrow_base_height):

for j in range(arrow_base_width):

Both of these loop are used to print the shaft line and after execution of these loops the output becomes:

**

**

**

**

**

Note that the outer loop is executed 5 times as arrow_base_height=5 and the inner loop iterates two times for each iteration of outer loop because arrow_base_width is 2 and the print ('*', end='') statement keeps printing the asterisks whereas print() prints a new line after printing 2 asterisks at each line.

Next the program moves to the following loops:

for i in range(arrow_head_width):

   for j in range(arrow_head_width-i):

Both of these loop are used to print the arrow head and after execution of these loops the output becomes:

****

***

**

*

Note that the outer loop is executed 4 times as arrow_head_width=4 and the inner loop iterates 4 times in start and decrements one time at each iteration and the print ('*', end='') statement keeps printing the asterisks whereas print() prints a new line after printing 2 asterisks at each line.

So the entire output of this program is:

**

**

**

**

**

****

***

**

*

The screenshot of the program along with its output is attached.

${teks-lihat-gambar} mahamnasir

Other Questions