Question

Part 3: Arrows Write a python program that prompts the user for a number of columns,...

Part 3: Arrows

Write a python program that prompts the user for a number of columns, and them prints the pattern as seen below. You cannot assume that the user will supply positive numbers - if they misbehave you should re-prompt them until they do give you a positive number. Note that your program only has to run one time, but multiple runnings of the program are shown below:

How many columns? 3
*
 *
  *
 *
*
How many columns? 5
*
 *
  *
   *
    *
   *
  *
 *
*
How many columns? 10
*
 *
  *
   *
    *
     *
      *
       *
        *
         *
        *
       *
      *
     *
    *
   *
  *
 *
*
How many columns? -5
Invalid entry, try again!
How many columns? -2
Invalid entry, try again!
How many columns? 6
*
 *
  *
   *
    *
     *
    *
   *
  *
 *
*

Some hints:

  • Use separate while loops to make your program easier to understand. One while loop can be used for data validation (negative numbers), another can be used for one half of the arrow structure, and a third can be used for the second half. Break down your program into smaller chunks!
  • To produce the pattern shown it will be helpful to create a few accumulator variables. For example, look at the number of spaces before the star character in each line. Do you see a pattern that can be simulated using a while loop?

Now, upgrade your program so that it handles both left and right arrows. Validate your input in the same way you validate the # of columns. Here are a few sample runnings:

How many columns?

How many columns? 5
Direction? (l)eft or (r)ight: l
    *
   *
  *
 *
*
 *
  *
   *
    *
How many columns? 5
Direction? (l)eft or (r)ight: r
*
 *
  *
   *
    *
   *
  *
 *
*
How many columns? 3
Direction? (l)eft or (r)ight: apple
Invalid entry, try again!
How many columns? 4
Direction? (l)eft or (r)ight: l
   *
  *
 *
*
 *
  *
   *
0 0
Add a comment Improve this question Transcribed image text
Answer #1
col = int(input('How many columns:'))
while col <= 0:
    print('Invalid entry, try again!')
    col = int(input('How many columns:'))

d = input('Direction? (l)eft or (r)ight: ').lower()
while d not in 'lr':
    print('Invalid entry, try again!')      
    d = input('Direction? (l)eft or (r)ight: ').lower()

spaces = 0 if d == 'r' else (col - 1)

for i in range(col):
    print(' '*spaces + '*')
    spaces += 1 if d == 'r' else -1

spaces = 1 if d == 'l' else (col - 2)
for i in range(col-1, 0, -1):
    print(' '*spaces + '*')
    spaces += -1 if d == 'r' else 1

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Part 3: Arrows Write a python program that prompts the user for a number of columns,...
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
  •   Write codes that will produce the screen as shown Firstly, the program prompts user to...

      Write codes that will produce the screen as shown Firstly, the program prompts user to enter his name. Then it will display Hello <user name>. Next it prints out a menu which contains 5 options (1- add two integers, 2- add two strings, 3- compute factorial, 4- reverse a string, 5- quit program). Code all 5 tasks appropriately. When the task is completed (not including quitting program of course), the menu pops up again and asks user to try...

  • In Java - Write a program that prompts the user to enter two integers and displays...

    In Java - Write a program that prompts the user to enter two integers and displays their sum. If the input is incorrect, prompt the user again. This means that you will have a try-catch inside a loop.

  • C++ only Implement a program that prompts a user to enter a number N where N...

    C++ only Implement a program that prompts a user to enter a number N where N is a positive number. The program must validate a user input and ask a user to re-enter until an input is valid. Implement a function that pass N as an argument and return a result of calculates N! (N-factorial): The function must use loop for the implementation. Hint: Factorial: N! = N * (N-1) * (N-2) * …. * 1 ( This is only...

  • in python 5.23 Login Create a program that prompts the user for a username and password....

    in python 5.23 Login Create a program that prompts the user for a username and password. The correct username should be python and the correct password should be csci 135. See below for example output. (Different messages depending on if user name or password are correct.) Only allow the user to make 3 attempts. Welcome to my secret program! Please enter your username: Trish Please enter your password: Duce Invalid username and password Please enter your username: Trish Please enter...

  • python program 6 Write an input validation loop that asks the user to enter a number...

    python program 6 Write an input validation loop that asks the user to enter a number in the range of 100 through 1000? 7 Write an input validation loop that asks the user to enter ‘Y’, ‘y’, ‘N’, or ‘n’? 8 How many times the following loop will repeat? cnt = 0    while  cnt != 5: print(cnt) cnt = cnt + 2

  • Write a C program that prompts the user for the number for times the user wants...

    Write a C program that prompts the user for the number for times the user wants to flip a coin. Your program will flip a virtual coin that number of times and return the following: 1) The number of times heads occurred. 2) The number of times tails occured. Then take the coin flip program and modify the random number generator to roll a single 6-sided dice.   DO NOT DISPLAY THE RESULTS OF EACH ROLL. 1) Ask the user how...

  • Your array shall have 3 rows and 3 columns (row and column indices are from 0...

    Your array shall have 3 rows and 3 columns (row and column indices are from 0 to 2). You shall initialize all elements of your array with character 'v' Player-1 is assigned 'X' symbol and player-2 is assigned 'O' symbol. Player-1 shall start the game and enter row index and column index where (s)he wants to place an 'X'. The board is updated and displayed. Now player-2 is asked to enter row and column indices If any player tries to...

  • Write a program that asks the user to type an even number or 111 to stop....

    Write a program that asks the user to type an even number or 111 to stop. When the user types an even number, display the message “Great Work”, and then ask for input again. When the user types an odd number, display the message “Try again” and ask the user to enter again. When the user enters the sentinel value of 111 the program ends I've attempted this but it went horribly wrong. we are only suppose to use while,...

  • write a c++ program that prompts a user for a number then attempts to allocate an...

    write a c++ program that prompts a user for a number then attempts to allocate an array of as many integers as the user enters. In other words, the program might prompt the user with a line like: “How many integers would you like to allocate?” It should then allocate as many integers as the user enters. The program should then wait for the user to press enter before deleting the allocated array and quitting. We will use this time...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

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