Question
The program interface in PYTHON This program should accept two strings from the user. The first string specifies where on the targets the program should
The program interface in PYTHON
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 +4-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 X then it appear as follows:
Hit string: -2+3+4-1 Marker: X | | 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 dont 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[0] + 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 + or -.
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_row. 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, a |, 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 dont have to print out a hit marker. If index_col == 0 that indicates we are at the middle of our target horizontally, and thus we should print out a |. Finally, ff index_row == 0 that indicates we are at the middle of our target vertically, and thus we should print out a -.
Example Walkthrough
Lets walk through part of an example. Say that the user ran the program and gave the target string +4+4+4+2+3+1+0-2-6-4 and the character #.
The code would grab the initial hit x/y as +4 and +4. Then, the outer loop would begin with index_row as +5, and then the inner loop would iterate from -7 to +7, printing out one character at a time. Since there are not hits on row +5, no #s would print. After the inner loop finishes iterating, the outer loop would repeat again, with index_row now at +4. The inner loop would begin iterating, and would print out a normal target up until the index_column value reaches +4. At this point, index_row == current_hit_y 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_x and current_hit_y 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: # | # | # | # | # |# -------#------- #| # | # | # | # |
Hit string: +7+5+0+3-4+0+4-3 Marker: + | + | + | | ---+---|------- | | | + | |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started