-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionsS.py
295 lines (207 loc) · 6.46 KB
/
FunctionsS.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 5 17:19:33 2018
@author: John Stewart
FUNCTIONS
"""
#%%
# This program demonstrates a function.
# First, we define a function named message.
def message():
print('I am Arthur')
print('King of the Britons')
# Call the message function.
message()
#%%
# This program has two functions. First we
# define the main function.
def main():
print('I have a message for you.')
message()
print('Goodbye!')
# Next we define the message function.
def message():
print('I am Arthur')
print('King of the Britons.')
# Call the main function.
main()
#%%
"""
This will cause an error! WHY?
"""
# Definition of the main function.
def main():
get_name()
print('Hello', name) # This causes an error!
# Definition of the get_name function.
def get_name():
name = input('Enter your name: ')
# Call the main function.
main()
#%%
# This program demonstrates an argument being
# passed to a function.
def main():
value = 5
show_double(value)
# The show_double function accepts an argument
# and displays double its value.
def show_double(number):
result = number * 2
print(result)
# Call the main function.
main()
#%%
# This program demonstrates a function that accepts
# two arguments.
def main():
print('The sum of 12 and 45 is')
show_sum(12, 45)
# The show_sum function accepts two arguments
# and displays their sum.
def show_sum(num1, num2):
result = num1 + num2
print(result)
# Call the main function.
main()
#%%
"""
Python can use keyword arguments. When a function argument is passed to a function, within the
function it is passed to, it is called a parameter. In addition to matching function arguments
and parameters, python allows for writing an argument in this format:
parameter_name=value
An argument written with this syntax is known as a kwyword argument.
"""
# This program demonstrates keyword arguments.
def main():
# Show the amount of simple interest using 0.01 as
# interest rate per period, 10 as the number of periods,
# and $10,000 as the principal.
show_interest(rate=0.01, periods=10, principal=10000.0)
# The show_interest function displays the amount of
# simple interest for a given principal, interest rate
# per period, and number of periods.
def show_interest(principal, rate, periods):
interest = principal * rate * periods
print('The simple interest will be $', \
format(interest, ',.2f'), \
sep='')
# Call the main function.
main()
#%%
# This program demonstrates passes two strings as
# keyword arguments to a function.
def main():
first_name = input('Enter your first name: ')
last_name = input('Enter your last name: ')
print('Your name reversed is')
reverse_name(last=last_name, first=first_name)
def reverse_name(first, last):
print(last, first)
# Call the main function.
main()
#%%
# This program demonstrates passing two string
# arguments to a function.
def main():
first_name = input('Enter your first name: ')
last_name = input('Enter your last name: ')
print('Your name reversed is')
reverse_name(first_name, last_name)
def reverse_name(first, last):
print(last, first)
# Call the main function.
main()
#%%
# This program demonstrates what happens when you
# change the value of a parameter.
def main():
value = 99
print('The value is', value)
change_me(value)
print('Back in main the value is', value)
def change_me(arg):
print('I am changing the value.')
arg = 0
print('Now the value is', arg)
# Call the main function.
main()
#%%
# This program demonstrates keyword arguments.
def main():
# Show the amount of simple interest using 0.01 as
# interest rate per period, 10 as the number of periods,
# and $10,000 as the principal.
show_interest(rate=0.01, periods=10, principal=10000.0)
# The show_interest function displays the amount of
# simple interest for a given principal, interest rate
# per period, and number of periods.
def show_interest(principal, rate, periods):
interest = principal * rate * periods
print('The simple interest will be $', \
format(interest, ',.2f'), \
sep='')
# Call the main function.
main()
#%%
# This program demonstrates passes two strings as
# keyword arguments to a function.
def main():
first_name = input('Enter your first name: ')
last_name = input('Enter your last name: ')
print('Your name reversed is')
reverse_name(last=last_name, first=first_name)
def reverse_name(first, last):
print(last, first)
# Call the main function.
main()
#%%
# Create a global variable.
my_value = 10
# The show_value function prints
# the value of the global variable.
def show_value():
print(my_value)
# Call the show_value function.
show_value()
#%%
# Create a global variable.
number = 0
def main():
global number
number = int(input('Enter a number: '))
show_number()
def show_number():
print('The number you entered is', number)
# Call the main function.
main()
"""
Comment out the "global number line
"""
#%%
"""
ACTIVITY#1
A company pays a quaterly bonus to its workiers along with retirement benefits
in the form 5% of each employees gross pay. Both go to the employees retirement
plan. Write a progam that calculates the company's contribution to an employee's
retirement account for the year. Use 2 functions and call them.
Get the eimployee's annual gross pay.
Get the total amount of bonuses paid to the employee.
Calculate and display the contribution for gross pay.
Calculate and display the contribution for bonuses.
"""
#%%
def main():
global bonus_pay, bonus_bonus, retirement_pay, total_pay
company_percent = 0.05
grosspay = int(input('What is your annual Gross Pay? '))
bonus = int(input('How much did you get in bonuses? '))
bonus_pay = grosspay * company_percent
bonus_bonus = bonus * company_percent
retirement_pay = bonus_pay + bonus_bonus
total_pay = grosspay + bonus
display()
def display():
print('Your businesses contribution for you retirement account is $', retirement_pay)
print('Your pay before business contributes is $', total_pay)
main()