Python代写:CS177 Lab 1 Summer2018

request Description

In this request you will write and submit the lab1.py program. Complete and submit the file by
the deadline to receive full credit.

Before you Begin

Carefully read the “Coding Standards and Guidelines” shown on the last page of this request.
Your Python programs should follow these standards and guidelines this semester.

Problem Description

The NASA training academy wants you to expand your tennis ball movement program to determine the distance a tennis ball falls within Earth’s gravity over time. Given an initial vertical velocity of a tennis ball (meters/second), and a specified time interval (seconds), they want to calculate the position and velocity of a tennis ball at each second of the interval. You can assume the horizontal velocity is zero, since the ball is only moving vertically, and that it does not hit the ground or any obstacles. We also assume the acceleration in the vertical direction is fixed at 9.81 m/s2 due to falling within Earth’s gravity. The ball’s initial velocity may be either positive or negative (ie: falling or rising).

Using the Python IDLE editor, write a program that prompts the user for 2 values:

  • the initial vertical velocity of the tennis ball
  • the time at which the position of the ball is required

Using the values entered by the user, calculate and display the position, and vertical velocity of the ball at each second within the user specified time interval.

Steps for This request

  1. Setup your Python file lab1.py with an appropriate comments header
  2. Organize lab1.py using comments for each block of planned code
  3. In each block, write the necessary Python code to complete the task
  4. Save, test, then upload your completed lab1.py Python program to Blackboard

TO DO #1: Setup Your Python Program File lab1.py

In Python, the # sign indicates a comment, which documents and organizes your code. Python does not
execute these commented lines; they are used to make the program easier to understand. In the Python IDLE editor, open a new program window.

TO DO #2: Organize lab1.py using comments

There are several parts to this program. Below your program header, enter additional comments in your Python program identifying each part. Make sure to leave blank spaces between each comment section.

TO DO #3: Complete the Code

Write appropriate Python code in each section to complete the program by entering commands within the
corresponding comment blocks. You will need the following formulas to determine the position of a moving
object given an initial velocity, the acceleration of gravity and time:

1
2
3
4
5
6
7
8
d = (Vi * t) + 1/2 * a * T^2
Vt = Vi + a * T
Where:
• 𝑑 = current position (distance traveled)
• 𝑣% = initial velocity
• 𝑡 = the current time
• 𝑣- = velocity at the current time
• 𝑎 = acceleration of gravity

NOTE: Use the round() function to display calculated results with 2 decimal places: round(d, 2)

TO DO #4: Save and Submit your Completed Python File on Blackboard

Save your Python program using the file name lab1.py, and submit it before the deadline for full credit.

  1. Click the Lab 1 request on Blackboard
  2. Click the “Attach File” button
  3. Attach your lab1.py Python file
  4. Click submit to complete this request

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#
# CS 177 – lab1.py
# {insert your name, and Purdue ID here}
# Following Coding Standards and Guidelines
# This program simulates the velocity and position
# of a tennis ball for each second within a time frame
# specified by the user
#

# Display the description of the program
print('This program simulates the movement of a tennis ball', end='\n')
print('==================================================================', end='\n')
print('It prompts the user for two values, the initial vertical velocity', end='\n')
print(' of the ball and time, then computes the position and velocity of', end='\n')
print(' the ball at each second.', end='\n')
print('==================================================================', end='\n')
print('', end='\n')

# Acceleration in the vertical direction
a = 9.81

# Prompt the user for input values
vi = float(input('Please enter the initial vertical velocity (meters per second): '))
time = int(input('Please enter the travel time (seconds): '))

print('\nTable of Results:', end='\n')
print('====================================================', end='\n')
print('Time (s) Position (m) Velocity (m/s)', end='\n')
print('====================================================', end='\n')

# Define a for loop representing each second of the time interval
for t in range(0, time + 1):
# Calculate the position and velocity of the tennis ball
# for the current second interval
d = vi * t + 0.5 * a * t * t
vt = vi + a * t

# Display the current time interval and the calculated position
# and vertical velocity
print('{0: 4d} {1: 13.2f} {2: 17.2f}'.format(t, round(d, 2), round(vt, 2)))