Question: Below is the code. The line is supposed to go in one direction and then another (zigzag).While I am able to get the asterisk's to

Below is the code.  The line is supposed to go in one direction and then another (zigzag).While I am able to get the asterisk's to go in one direction, I am unable to figure out what I did wrong to have them go back in the opposite direction based on the instructions.  In other words, they will go to the right but do not change direction:

 

while True:

   print ('Enter an integer between 5 and 25.')
   try:
       userNum = int(input())
   except ValueError:
       print ('You must enter an integer!')
   if userNum < 5:
           continue
   elif userNum > 25:
           continue
   else:
           break
import time, sys
indent = 0
indentIncreasing = True

try:
           while True: # The main program loop.
               if indent == 0:
                   print ('' * indent, end='')
                   print ('*' * userNum)
               indent = indent + 1
               print (' ' * indent, end ='')
               print ('*' * userNum)
               time.sleep(0.1) # Pause for 1/10 of a second.
               
               if indentIncreasing == False:# Increase the number of spaces:
                   indent = indent - 1
                   if indent == 0:
                       print (' ' * indent, end='')
                       print ('*' * userNum)
                       indentIncreasing = True
                       indent = indent + 1
   
           
           else:
                    indent = indent +1
           if indent == 20:
                       print (' ' * indent, end='')
                       print ('*' * userNum)
                       increasingIndent = False
                       indent = indent - 1

           else:#Decrease the number of spaces:
                   indent = indent - 1
           if indent == 0: #Change direction
                       print (' ' * indent, end='')
                       print ('*' * userNum)
                       indentIncreasing = True
               

                               
except KeyboardInterrupt:
           sys.exit()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The issue with the code is that the logic for changing the direction of the asterisks is not correct... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!