CMPSC 131 Program Examples – Chapter 2
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CMPSC 131
Program Examples – Chapter 2
Type in the following program samples. Save them as python scripts with the suggested names. Run the scripts and compare the output against the expected output.
Program 2-6 (comment2 . py)
2 print('123 Ful1 Ci rcle Drive1) # Di splay the address
# Display the city, state, and ZIP.
Program Output
Kate Austen
123 Full Circle Drive
Asheville, NC 28899
Program 2-8 (vari abl e_demo2. py)
1 # Create two variables: top_speed and di stance.
2 top_speed = 160
3 distance = 300
5 # Display the values referenced by the variables
6 print('The top speed is')
7 print(top_speed)
8 print('The di stance traveled is')
9 print(di stance)
Program Output
The top speed 1s
160
The di stance traveled is
300
Program 2-11 (stri ng_variabl e . py)
1 # Create variables to reference two strings.
2 flrst_name = 'Kathryn'
3 last_name = 'Marino'
5 # Display the values referenced by the variables.
6 print(first_name, lastjame)
Program Output
Kathryn Marino
1 # This program demonstrates variable reassignment.
2 # Assign a value to the dollars variable.
3 dollars = 2.75
4 print('I have', dollars, 'in my account.')
6 # Reassign dollars so it references
7 # a different value.
8 dollars = 99.95
9 print('But now I have', dollars, 'in my account!')
Program Output
I have 2.75 in my account.
But now I have 99.95 1n my account!
Program 2-12 (stri ng_i nput. py)
1 # Get the user's first name.
2 first_name = input('Enter your first name:')
3
4 |
# Get the user's last name. |
|
5 |
last_name |
=input('Enter your last name:') |
6 |
|
|
7 # Print a greeting to the user.
8 pr1nt('Hello', first_name, last_name)
Program Output (with input shown in bold)
Enter your first name: Vinny (Enter)
Enter your last name: Brown [Enter]
Hello Vinny Brown
1 # Assign a value to the salary variable.
2 salary = 2500.0
3 # Assign a value to the bonus variable.
4 bonus = 1200.0
7 # Calculate the total pay by adding salary
8 # and bonus. Assign the result to pay.
9 pay = salary + bonus
10
10 # Di splay the pay.
11 print('Your pay is', pay)
Program Output
Your pay Is 3700.0
Program 2-15 (sale_price . py)
4 # Get the item's original price.
5 original_price = float(input("Enter the item's original price: ”))
7 # Calculate the amount of the discount.
8 discount 二 original_price * 0.2
10 # Calculate the sale price.
11 sale_price = orig1nal_price - discount
12
12 # Display the sale price.
13 print('The sale price Is', sale_price)
Program Output (with input shown in bold)
Enter the item's original price: 100.00[Enter]
The sale price is 80.0
.1
11
12
13
14
15
16
17
# Get a number of seconds from the user.
total_seconds = float(1nput('Enter a number of seconds:'))
# Get the number of hours, hours = total_seconds /I 3600
# Get the mi nutes =
# Get the seconds =
number of remaining minutes. (total_seconds // 60) % 60
number of remaining seconds.
total_seconds % 60
# Di splay
print('Here is the time 1n hours, minutes, and seconds:') print('Hours:', hours)
print('Minutes:1, minutes)
print('Seconds:, seconds)
the results.
Program Output (with input shown in bold)
Enter a number of seconds: 11730【Enter)
Here Is the time 1n hours, minutes, and seconds:
Program 2-18
(future_value.py)
# Get the deslred future value. future_value = float(1nput('Enter the desired
:
11
12
13
14
# Get the annual 1nterest rate = float(input('Enter
# Get the number of years years
rate.
the annual interest
rate:'))
appreciate.
that the money will
int(input(!Enter the number of years the money will grow: 1))
# Calculate the amount needed to deposit. present_value 二 future_value / (1.0+ rate)**years
# Display the amount needed to deposit.
print('You wi11 need to deposit this amount:' 5 present_value)
the desired future value: 10000.0[EnterJ
the annual interest rate: 0.05〔Enter]
11 grow: 10 [Enter]
2022-09-18