-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstPrograms-FunctionsStudents.py
209 lines (138 loc) · 4.6 KB
/
FirstPrograms-FunctionsStudents.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 16 17:06:03 2018
@author: John Stewart
"""
# First Programs and Functions -*-
"""
Simple Programs and Computations
Python Basics
Numeric Datatypes
Arithmetic operations
variables, expressions, and statements
input statements
"""
"""
We will mainly use the following window panes: IPython Console, Editor,
File Explorer, and Object Inspector.
#%% breaks up the Editor document into cells.
The green triangle in the tool
bar executes the entire file (after saving it), Ctrl-Enter (Command-Return on a
Mac) executes only the cell that the cursor is in (but does not save).
Instructions on changing working directory in Spyder:
At the top on the right you will see a path, the working directory. To its right is a file
folder.
Click it and you can change the working directory. When you do, you
can click the icon to the right of that and set that path as the IPython
console's new working directory.
Then all the panes: Editor, IPython Console,
and File Explorer are pointed to this current working directory.
In the more
recent versions of Spyder, this button has been eliminated and the editor and
IPython console are automatically set to the current working directory.
"""
#%%
"""A Simple Function"""
def hello():
""" prints hello, world """
print("Hello, world!")
#%%
"""Variable Assignment """
# Create two variables: top_speed and distance.
top_speed = 160
distance = 300
# Display the values referenced by the variables.
print('The top speed is')
print(top_speed)
print('The distance traveled is')
print(distance)
#%%
#Example of another funtion
def areacircle(radius):
""" Computes the area of a circle of the given radius """
area = 3.14*radius**2
print("The area of a circle of radius",radius,"is", area)
#%%
"""
ACTIVITY 1:
Compute the area of a Triangle
Write a function called triangArea to compute the area
of a triangle: formula is area = .5*b*h.
Output should look like:
The area of a triangle of base 3 and height 5 is 7.5
You can test your function by executing the following code:
"""
def triangArea(b,h):
area = b*h*.5
print("The area of a triangle with a height of",h,"and a base of" ,b,"is ", area,".")
#%%
# The following will test areatriangle()
#triangArea(3,5)
#triangArea(2,20)
#%%
def mi_km(miles):
km = miles * 1.60934
print(miles, "miles", "is equal to", km, "kilometers")
"""
Convert miles to kilometers
1 mile = 1.60934 km
ACTIVITY 2:
WRITE A FUNCTION TO CONVERT KILOMETERS TO MILES
"""
def km_mi(km):
miles = km *0.621371
print(km, "Kilometers", "is equal to", miles, "Miles"
#%%
def fahrenheit_to_celsius(temp):
""" Converts Fahrenheit temperature to Celsius.
Formula is 5/9 of temp minus 32 """
# Note that this line is not executed
# end='' keeps print from starting a new line.
newTemp = 5*(temp-32)/9
print("The Fahrenheit temperature",temp,"is equivalent to",newTemp,end=' ')
print(" degrees Celsius")
"""
end=' ' eliminates the print statement carrying over to 2 lines. This addition will
print a blank and keep the statement on one line
"""
#%%
"""
ACTIVITY 3:
Write a function 'def celsius_to_fahrenheit' to convert Celsius
to Fahrentheit temperature. The formula is (9/5) times temp plus 32.
Print the output in the form:
The Celsius temperature 50.0 is equivalent to 122.0 degrees Fahrenheit.
"""
#%%
"""
Solution:
"""
#%%
# Use the following will test the above function
celsius_to_fahrenheit(100)
celsius_to_fahrenheit(0)
celsius_to_fahrenheit(50.)
#%%
"""
We can write a function that takes input directly from the keyboard
"""
def name():
""" Input first and last name, combine to one string and print """
fname = input("Enter your first name: ")
lname = input("Enter your last name: ")
fullname = fname + " " + lname
print("Your name is:", fullname)
#%%
"""
ACTIVITY 4:
Add to the above name function to include the city and state where you live.
That is, ask two more questions to get the city and the state you live in.
Your run should look like the following (even if this is not the customary
way in your country):
Enter your first name: John
Enter your last name: Stewart
Enter the city you live in: Apollo
Enter the state you live in: PA
Your name is: John Stewart
You live in: Apollo,PA
"""