Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Python! Only while loops, pls! The program interface This program should accept two strings from the user. The first string specifies where on the

Use Python! Only while loops, pls!

image text in transcribedimage text in transcribedimage text in transcribed

The program interface This program should accept two strings from the user. The first string specifies where on the targets the program should display the "hits". The target that your program prints out should be a 15-character wide by 11-character tall target, as shown below: The user can tell the program where to put the hit markers on this target using this first input. Each hit should be represented with 4 characters, and the string could have more than one hit. Thus, the length of the string should always be a multiple of 4. Here is one example input string: -2+3+4-1 This input string represents two hits on the target (-2+3 and 14-1.). -2+3 means that on the x axis (side to side) the hit should be at negative 2 (two characters left of center) and on the y axis the hit should be at the third level above the middle. +4-1 means that on the x axis the hit should be four characters right of center and on the y axis the hit should be one level above the middle. The second input should be exactly one character long. It represents that character to put onto the target to mark the hit. For example, if the program were run with the first input as (-2+3+4-1) and the second input as the string then it appear as follows: Hit string: -2+3+4-1 Marker: X X Your code should check to ensure that the hit string has a length that is greater than 3, and a multiple of 4. If this is not the case, it should print please provide a valid hit string. and then continue to ask for the input until a valid one is received. It should also check to ensure that the hit marker has a length of exactly 1. If not, it should print Please provide a valid marker. and then continue to ask for the input until a valid one is received. Printing the Target You are not allowed to use string multiplication for this assignment. Instead, in order to print out the target, I recommend that you use a while loop within a while loop. The "outer" while loop will be responsible for iterating through each row. For this, you can set up a while loop that begins at +5 and iterates until -5. Within this "outer" loop, you can have an "inner" loop that will print out each character on a row, one at a time. There is a rule that will make this easier. The hit marker input string will: (A) Never specify multiple hits on the same row of the target (B) Specify the hits in order, from top to bottom (C) Every hit string will have at LEAST one hit Due to this, you can loop through the hit string in-order, and you don't have to concern yourself with printing out multiple hits on the same line. Before we even get to setting up our while loops to print out the rows and characters of this target, we should grab the X/Y position of the first hit and save them into variables. This would look something like: current_hit_x = int(target_string[@] + target_string[1]) current_hit y = int(target_string[2] target_string[3]) + We need two characters for each because we need to account for the dor - Next, you should write the loops for printing this out. Since string multiplication is not allowed, you will have to write TWO while loops, one inside of the other. The first (outer) loop will be responsible for iterating an index variable from +5 to -5. For example, we could call this index_rom. EACH time this loop iterates, we want another loop inside to iterate between-7 and +7, so that we can check each location at the current row. We could call this index_column). Each time the inner loop iterates, we need to decide to print one of 4 possible characters: A space, al,an, or a hit marker. We would print a hit marker whenever index_row == current_hit_y AND index_column == current_hit_x]. If that is not true, then we know we don't have to print out a hit marker. If index_col == @ that indicates we are at the "middle" of our target horizontally, and thus we should print out al Finally, ffindex_row == e) that indicates we are at the "middle" of our target vertically, and thus we should print out a For EACH row, loop from -7 to +7 with an INNER while loop If the number never matches the X coord of the current hit, print a space, unless X coord is zero, print a vertical bar. Example Walkthrough Lets walk through part of an example. Say that the user ran the program and gave the Begin top-left corner. Get the X and Y position of the | target string +4+4+4+2+3+1+9-2-6-4 and the first hit in the hit string to begin. I character 1 The code would grab the initial hit x/y as 4 1 X When you reach a row that has a hit, make sure to shift to and 44). Then, the outer loop would begin Outer loop, use while loop to IX the next hit in your hit string! with index_row as 5, and then the inner loop iterate from 5 (top row) to -5 (bottom row) would iterate from to 2, printing out one 1 For middle row, print dashes character at a time. Since there are not hits instead of spaces. A hit CAN on row 3, no s would print. After the inner 1 be on the target cross loop finishes iterating, the outer loop would 1 repeat again, with index_row) now at 9). The 1 inner loop would begin iterating, and would print out a normal target up until the index_column) value reaches 14). At this point, index_row == current_hity AND index_column == current_hit_x, thus a should be printed. Right after it prints this hashtag, the program should go ahead and change current_hit_ and current_hity to the next values in the hit string. Then, continue looping. Basically, this idea repeats for the rest of the target. Rows with no hits end up printing as normal, and rows with hits must print the marker at the correct spot, and then move the current_hit_x) and current_hit_y variables to the next values (unless the end of the string has been reached). More Examples Hit string: +5+5+4+4+3+3+2+2+1+1+0+0-1-1-2-2-3-3-4-4-5-5 Marker: # # # # # # # # F

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions