-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_3_Solution.py
More file actions
184 lines (160 loc) · 6.69 KB
/
Lab_3_Solution.py
File metadata and controls
184 lines (160 loc) · 6.69 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
##############################################################
# Lab 3 Solution #
# This program reads the user's name, birth month and year. #
# It will tell them what season they were born and if the #
# year was a leap year or not. #
# Then the program will ask the user for their yearly salary #
# and display it in terms of monthly, daily and hourly #
# amounts. #
# The program will contine to let the user enter names, #
# birthdate info and salary until they are done. They will #
# enter a name of "zzz" to signal they are done. #
#############################################################
def main():
# Priming read for the sentinel-controlled while loop
# Read the first user's name or "zzz" to quit
name = input('Enter your name or "zzz" to quit: ')
# Check for the sentinal value of "zzz"
while name != "zzz":
# Get the user's birth month
birth_month = get_month()
# Get the user's birth year
birth_year = get_year()
# Get the season
season = find_season(birth_month)
# Call function to determine if the year was a leap year or not
if is_leap_year(birth_year):
leap_year = ' was a leap year'
else:
leap_year = ' was not a leap year'
# Display message to the user
print("\nHello, ", name, "! You were born in the ", season,
" and ", birth_year, leap_year, '.', sep='')
print()
pennies = int(input("How many pennies are in your penny jar? "))
penny_jar(pennies)
# Update read for the sentinel-controlled while loop
# Read the next user's name or "zzz" to quit
name = input('\nEnter your name or "zzz" to quit: ')
################################################
# Function name: get_month #
# Input: none #
# Output: month as an integer #
# Purpose: This function gets the month in #
# which the user was born #
################################################
def get_month():
# Read user's birth month and validate it
month = int(input("Enter the month you were born as an integer: "))
while month < 1 or month > 12:
print("Invalid month: must be between 1 and 12")
month = int(input("Enter the month you were born as an integer: "))
# Return the valid month
return month
################################################
# Function name: get_year #
# Input: none #
# Output: year as an integer #
# Purpose: This function gets the year in #
# which the user was born #
################################################
def get_year():
# Read user's birth year and validate it
year = int(input("Enter the year you were born (yyyy): "))
while year <= 0:
print("Invalid year: must be greater than zero")
year = int(input("Enter the year you were born (yyyy): "))
# Return the valid year
return year
################################################
# Function name: find_season #
# Input: month as an integer #
# Output: season as an integer #
# Purpose: This function gets the season the #
# month is in #
################################################
def find_season(month):
# Decide which season the month is in
# 12, 1, 2 = winter
# 3, 4, 5 = spring
# 6, 7, 8 = summer
# 9, 10, 11 = fall
if month <= 2 or month == 12:
season = "winter"
elif month <= 5:
season = "spring"
elif month <= 8:
season = "summer"
else:
season = "fall"
# Return the season
return season
################################################
# Function name: is_leap_year #
# Input: year as an integer #
# Output: true if year is a leap year #
# false if it is not #
# Purpose: This function determines if a year #
# is a leap year or not #
################################################
def is_leap_year(year):
# Determine if it is a leap year
# If year is evenly divisible by 4
# check to see if it is a century year
if year % 4 == 0:
# If year is a century year
# check to see if it is evenly divisible by 400
if year % 100 == 0:
# At this point, if year is evenly divisible
# by 400 it is a leap year
if year % 400 == 0:
leap_year = True
else:
# Century year that is not evenly
# divisible by 400 is not a leap year
leap_year = False
else:
# A year evenly divisible by 4 that is
# not a century year is a leap year
leap_year = True
else:
# A year that is not evenly
# divisible by 4 is not a leap year
leap_year = False
return leap_year
##################################################
# Function name: penny_jar #
# Input: number of pennies #
# Output: none #
# Purpose: This function calculates the number #
# of dollars, quarters, dimes, nickels #
# and pennies they will get when they #
# cash in the pennies #
##################################################
def penny_jar(total_pennies):
# Preserve the original total penny amount
pennies = total_pennies
# Calculate dollars
dollars = pennies // 100
# Subtract out the dollars from the pennies
pennies = pennies - (dollars * 100)
# Calculate quarters
quarters = pennies // 25
# Subtract out the quarters from pennies
pennies = pennies - (quarters * 25)
# Calculate dimes
dimes = pennies // 10
# Subtract out the dimes from pennies
pennies = pennies - (dimes * 10)
# Calculate nickels
nickels = pennies // 5
# Subtract out the nickels from pennies
pennies = pennies - (nickels * 5)
# Display the amount in denominations
print(total_pennies, " pennies comes to the following:")
print("\tdollars: ", dollars)
print("\tquarters: ", quarters)
print("\tdimes: ", dimes)
print("\tnickels: ", nickels)
print("\tpennies: ", pennies)
main()