In: Computer Science
Study and consider the following Python code, which tracks the movement of a balloon given the wind direction and distance
#
Establish Balloon Behavior(Parts, Direction, Distance) n = int(input('Input number of parts: ')) northDistance = 0 eastDistance = 0 balloonOutput = [] for i in range(0, n): outputLine = [] print('Part %d- ' % (i + 1), end='') direction = input('What direction is the balloon heading? type N, E, S or W ') direction = direction.upper() while len(direction) != 1 or \ not(direction == 'N' \ or direction == 'E' \ or direction == 'S' \ or direction == 'W'): direction = input('Bad input, try again- What direction is the balloon heading? type N, E, S or W ') direction = direction.upper() outputLine += [direction] distance = int(input('How many inches did it travel? ')) while distance <= 0: distance = int(input('Bad input, try again- How many inches did it travel? ')) outputLine += [distance] balloonOutput += [outputLine] if direction == 'N': northDistance = northDistance + distance elif direction == 'E': eastDistance = eastDistance + distance elif direction == 'S': northDistance = northDistance - distance elif direction == 'W': eastDistance = eastDistance - distance print('Part Direction Distance') print('____ _________ ________') for i in range(0, n): print('%2d %s %d' % (i+1, balloonOutput[i][0], balloonOutput[i][1])) verticalDirection = '' verticalLocation = 0 if northDistance > 0: verticalDirection = 'N' verticalLocation = northDistance elif northDistance < 0: verticalDirection = 'S' verticalLocation = -northDistance horizontalDirection = '' horizontalLocation = 0 if eastDistance > 0: horizontalDirection = 'E' horizontalLocation = eastDistance elif eastDistance < 0: horizontalDirection = 'W' horizontalLocation = -eastDistance finalDirection = '' print('~~~~~Final Location of Balloon~~~~~~') if verticalLocation != 0: print(' %s %d' % (verticalDirection, verticalLocation), end='') if horizontalLocation != 0: print(' %s %d' % (horizontalDirection, horizontalLocation)) if verticalLocation ==0 and horizontalLocation == 0: print(' NO CHANGE') |
1) Modify the above code to no longer prompt for input (hence remove prompts and input function calls). Make the assumption that the data file always has valid inputs. Remove all unnecessary (not needed for use with data file) code.
2) Open, read, and close the provided Comma Separated Value (CSV) file “BalloonMovements.csv”, which contains
BallonMovements.cvs
4 | e |
3 | n |
3 | W |
11 | N |
2 | w |
5 | w |
5 | n |
1 | s |
4 | N |
3 | n |
3.) Add more code comments explaining your new code and what the remaining original code does. Using code comments (#), comment each major section and specifically what the following lines do:
Output after running the input file is:
|
ANSWER :
GIVEN THAT
import csv
# Establish Balloon Behavior(Parts, Direction,
Distance)
# Initialize balloon start position as N = 0 and E = 0
northDistance = 0
eastDistance = 0
# List to store balloon output
balloonOutput = []
# Open BalloonMovements file
with open("BalloonMovements.csv") as bm:
# Get values from csv file delimited by tab
csvReader = csv.reader(bm, delimiter="\t")
n = csvReader.line_num # Number of lines in file
# Read each row from file
for row in csvReader:
# Declare list of output lines
outputLine = []
# Convert Direction to uppercase letter
direction = row[1].upper()
# insert direction in outputLine list
outputLine += [direction]
# Read distance from file
distance = int(row[0])
# Insert distance in outputLine
outputLine += [distance]
# Append outputLine list at the end of balloonOutput
list
balloonOutput += [outputLine]
if direction == 'N':
northDistance = northDistance + distance
elif direction == 'E':
eastDistance = eastDistance + distance
elif direction == 'S':
northDistance = northDistance - distance
elif direction == 'W':
eastDistance = eastDistance - distance
print('Part Direction Distance')
print('____ _________ ________')
for i in range(0, n):
print('%2d %s %d' % (i+1, balloonOutput[i][0],
balloonOutput[i][1]))
verticalDirection = ''
verticalLocation = 0
if northDistance > 0:
verticalDirection = 'N'
verticalLocation = northDistance
elif northDistance < 0:
verticalDirection = 'S'
verticalLocation = -northDistance
horizontalDirection = ''
horizontalLocation = 0
if eastDistance > 0:
horizontalDirection = 'E'
horizontalLocation = eastDistance
elif eastDistance < 0:
horizontalDirection = 'W'
horizontalLocation = -eastDistance
finalDirection = ''
print('~~~~~Final Location of Balloon~~~~~~')
if verticalLocation != 0:
print(' %s %d' % (verticalDirection, verticalLocation),
end='')
if horizontalLocation != 0:
print(' %s %d' % (horizontalDirection,
horizontalLocation))
if verticalLocation ==0 and horizontalLocation ==
0:
print(' NO CHANGE')
SCREEN SHOT:
OUTPUT :