Skip to content

Commit f779ef1

Browse files
committed
HW
HW
1 parent 27cab7b commit f779ef1

3 files changed

Lines changed: 282 additions & 24 deletions

File tree

Python Fundamentals/Module_5_Required_FINAL_Project_Python_Fundamentals.ipynb

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,42 +85,89 @@
8585
},
8686
{
8787
"cell_type": "code",
88-
"execution_count": 1,
89-
"metadata": {
90-
"collapsed": true
91-
},
92-
"outputs": [],
88+
"execution_count": 4,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
96+
" Dload Upload Total Spent Left Speed\n",
97+
"100 161 100 161 0 0 570 0 --:--:-- --:--:-- --:--:-- 570\n",
98+
"\n",
99+
"Enter the name of an element: oxygen\n",
100+
"Enter the name of an element: carbon\n",
101+
"Enter the name of an element: taeium\n",
102+
"Enter the name of an element: hydrogen\n",
103+
"Enter the name of an element: hydrogen\n",
104+
"Duplicate entry\n",
105+
"Enter the name of an element: carbon\n",
106+
"Duplicate entry\n",
107+
"Enter the name of an element: \n",
108+
"Invaild entry\n",
109+
"Enter the name of an element: awesomeium\n",
110+
"\n",
111+
"You got 60 % correct!\n",
112+
"You found: Oxygen Carbon Hydrogen\n",
113+
"These were incorrect: Taeium Awesomeium\n"
114+
]
115+
}
116+
],
93117
"source": [
94118
"# [] create Element_Quiz\n",
95119
"# [] copy and paste in edX assignment page\n",
96120
"\n",
97121
"\n",
98122
"def get_names():\n",
99-
" element_name = input(\"Enter the name of an element: \")\n",
123+
" element_name = input(\"\\nEnter the name of an element: \")\n",
100124
" count = 0\n",
101125
" entry = []\n",
102126
"\n",
103-
" while count < 5:\n",
127+
" while count <= 4:\n",
104128
" \n",
105129
" if element_name in entry :\n",
106130
" print(\"Duplicate entry\")\n",
107-
" elif element_name = \" \" or element_name = \"\": \n",
131+
" elif element_name == \" \" or element_name == \"\": \n",
108132
" print(\"Invaild entry\")\n",
109133
" else:\n",
110134
" entry.append(element_name)\n",
111135
" count += 1\n",
112-
" element_name = input(\"Enter the name of an element: \")\n",
136+
" \n",
137+
" if count <= 4: \n",
138+
" element_name = input(\"Enter the name of an element: \")\n",
113139
" return entry\n",
114140
"\n",
115141
"!curl \"https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/elements1_20.txt\" -o 'elements1_20.txt'\n",
116142
"\n",
117143
"elements_file = open('elements1_20.txt','r')\n",
118144
"elements = []\n",
119145
"\n",
120-
"for num in range(20):\n",
121-
" elements.append(---elements_file.lower().readline().strip())\n",
122146
"\n",
123-
"get_names()"
147+
"for num in range(20):\n",
148+
" elements.append(elements_file.readline().lower().strip())\n",
149+
"\n",
150+
"entry = get_names()\n",
151+
"\n",
152+
"\n",
153+
"found = []\n",
154+
"missing = []\n",
155+
"correct = 0\n",
156+
"for guess in entry:\n",
157+
" wrong = True\n",
158+
" for answer in elements:\n",
159+
" if answer == guess:\n",
160+
" found.append(guess)\n",
161+
" correct += 1\n",
162+
" wrong = False \n",
163+
" if wrong:\n",
164+
" missing.append(guess)\n",
165+
" \n",
166+
"score = correct*20 \n",
167+
" \n",
168+
"print(\"\\nYou got\",score,\"% correct!\")\n",
169+
"print(\"You found: \", \" \".join(found).title())\n",
170+
"print(\"These were incorrect: \", \" \".join(missing).title())"
124171
]
125172
},
126173
{
@@ -137,18 +184,6 @@
137184
"display_name": "Python 3",
138185
"language": "python",
139186
"name": "python3"
140-
},
141-
"language_info": {
142-
"codemirror_mode": {
143-
"name": "ipython",
144-
"version": 3
145-
},
146-
"file_extension": ".py",
147-
"mimetype": "text/x-python",
148-
"name": "python",
149-
"nbconvert_exporter": "python",
150-
"pygments_lexer": "ipython3",
151-
"version": "3.7.6"
152187
}
153188
},
154189
"nbformat": 4,
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Final Project Required Coding Activity \n",
8+
"Introduction to Python (Unit 2) Fundamentals \n",
9+
" \n",
10+
"All course .ipynb Jupyter Notebooks are available from the project files download topic in Module 1, Section 1. \n",
11+
"\n",
12+
"This activity is based on modules 1 - 4 and is similar to exercises in the Jupyter Notebooks **`Practice_MOD03_IntroPy.ipynb`** and **`Practice_MOD04_IntroPy.ipynb`** which you may have completed as practice.\n",
13+
"\n",
14+
"| **Assignment Requirements** |\n",
15+
"|:-------------------------------|\n",
16+
"|This program requires the use of **`print`** output and use of **`input`**, **`for`**/**`in`** loop, **`if`**, file **`open`**, **`.readline`**, **`.append`**, **`.strip`**, **`len`**. and function **`def`** and **`return`**. The code should also consider using most of the following (`.upper()` or `.lower()`, `.title()`, `print(\"hello\",end=\"\")` `else`, `elif`, `range()`, `while`, `.close()`) |\n",
17+
"\n",
18+
"\n",
19+
"## Program: Element_Quiz \n",
20+
"In this program the user enters the name of any 5 of the first 20 Atomic Elements and is given a grade and test report for items correct and incorrect. \n",
21+
"\n",
22+
"\n",
23+
"### Sample input and output: \n",
24+
"```\n",
25+
"list any 5 of the first 20 elements in the Period table\n",
26+
"Enter the name of an element: argon\n",
27+
"Enter the name of an element: chlorine\n",
28+
"Enter the name of an element: sodium\n",
29+
"Enter the name of an element: argon\n",
30+
"argon was already entered <--no duplicates allowed\n",
31+
"Enter the name of an element: helium\n",
32+
"Enter the name of an element: gold\n",
33+
"\n",
34+
"80 % correct\n",
35+
"Found: Argon Chlorine Sodium Helium \n",
36+
"Not Found: Gold \n",
37+
"``` \n",
38+
"\n",
39+
"\n",
40+
"### Create get_names() Function to collect input of 5 unique element names \n",
41+
"\n",
42+
"- The function accepts no arguments and returns a list of 5 input strings (element names) \n",
43+
"- define a list to hold the input\n",
44+
"- collect input of a element name \n",
45+
"- if input it is **not** already in the list add the input to the list \n",
46+
"- don't allow empty strings as input \n",
47+
"- once 5 unique inputs **return** the list \n",
48+
"\n",
49+
"\n",
50+
"### Create the Program flow \n",
51+
"\n",
52+
"#### import the file into the Jupyter Notebook environment \n",
53+
"\n",
54+
"- use `!curl` to download https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/elements1_20.txt\" as `elements1_20.txt` \n",
55+
"- open the file with the first 20 elements \n",
56+
"- read one line at a time to get element names, remove any whitespace (spaces, newlines) and save each element name, as lowercase, into a list \n",
57+
"\n",
58+
"\n",
59+
"#### Call the get_names() function \n",
60+
"\n",
61+
"- the return value will be the quiz responses list \n",
62+
"\n",
63+
"#### check if responses are in the list of elements \n",
64+
"Iterate through 5 responses \n",
65+
"- compare each response to the list of 20 elements\n",
66+
" - any response that is in the list of 20 elements is correct and should be added to a list of correct responses \n",
67+
" - if not in the list of 20 elements then add to a list of incorrect responses \n",
68+
"\n",
69+
"#### calculate the % correct \n",
70+
" \n",
71+
" - find the the number of items in the correct responses and divide by 5, this will result in answers like 1.0, .8, .6,... \n",
72+
" - to get the % multiple the calculated answer above by 100, this will result in answers like 100, 80, 60... \n",
73+
" - *hint: instead of dividing by 5 and then multiplying by 100, the number of correct responses can be multiplied by 20* \n",
74+
"\n",
75+
"#### Print output \n",
76+
"\n",
77+
"- print the Score % right \n",
78+
"- print each of the correct responses \n",
79+
"- print each of the incorrect responses \n",
80+
"\n",
81+
"\n",
82+
"### create Element_Quiz then paste code on edX submission page\n",
83+
"\n"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 4,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
96+
" Dload Upload Total Spent Left Speed\n",
97+
"100 161 100 161 0 0 570 0 --:--:-- --:--:-- --:--:-- 570\n",
98+
"\n",
99+
"Enter the name of an element: oxygen\n",
100+
"Enter the name of an element: carbon\n",
101+
"Enter the name of an element: taeium\n",
102+
"Enter the name of an element: hydrogen\n",
103+
"Enter the name of an element: hydrogen\n",
104+
"Duplicate entry\n",
105+
"Enter the name of an element: carbon\n",
106+
"Duplicate entry\n",
107+
"Enter the name of an element: \n",
108+
"Invaild entry\n",
109+
"Enter the name of an element: awesomeium\n",
110+
"\n",
111+
"You got 60 % correct!\n",
112+
"You found: Oxygen Carbon Hydrogen\n",
113+
"These were incorrect: Taeium Awesomeium\n"
114+
]
115+
}
116+
],
117+
"source": [
118+
"# [] create Element_Quiz\n",
119+
"# [] copy and paste in edX assignment page\n",
120+
"\n",
121+
"\n",
122+
"def get_names():\n",
123+
" element_name = input(\"\\nEnter the name of an element: \")\n",
124+
" count = 0\n",
125+
" entry = []\n",
126+
"\n",
127+
" while count <= 4:\n",
128+
" \n",
129+
" if element_name in entry :\n",
130+
" print(\"Duplicate entry\")\n",
131+
" elif element_name == \" \" or element_name == \"\": \n",
132+
" print(\"Invaild entry\")\n",
133+
" else:\n",
134+
" entry.append(element_name)\n",
135+
" count += 1\n",
136+
" \n",
137+
" if count <= 4: \n",
138+
" element_name = input(\"Enter the name of an element: \")\n",
139+
" return entry\n",
140+
"\n",
141+
"!curl \"https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/elements1_20.txt\" -o 'elements1_20.txt'\n",
142+
"\n",
143+
"elements_file = open('elements1_20.txt','r')\n",
144+
"elements = []\n",
145+
"\n",
146+
"\n",
147+
"for num in range(20):\n",
148+
" elements.append(elements_file.readline().lower().strip())\n",
149+
"\n",
150+
"entry = get_names()\n",
151+
"\n",
152+
"\n",
153+
"found = []\n",
154+
"missing = []\n",
155+
"correct = 0\n",
156+
"for guess in entry:\n",
157+
" wrong = True\n",
158+
" for answer in elements:\n",
159+
" if answer == guess:\n",
160+
" found.append(guess)\n",
161+
" correct += 1\n",
162+
" wrong = False \n",
163+
" if wrong:\n",
164+
" missing.append(guess)\n",
165+
" \n",
166+
"score = correct*20 \n",
167+
" \n",
168+
"print(\"\\nYou got\",score,\"% correct!\")\n",
169+
"print(\"You found: \", \" \".join(found).title())\n",
170+
"print(\"These were incorrect: \", \" \".join(missing).title())"
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"metadata": {},
176+
"source": [
177+
"Submit this by creating a python file (.py) and submitting it in D2L. Be sure to test that it works. Know that For this to work correctly in Python rather than Jupyter, you would need to switch to using import os rather than !curl. To convert !curl to run in the normal python interpreter try a method such as importing the os library and calling os.system(cmd) with your shell command in the cmd variable. \n"
178+
]
179+
}
180+
],
181+
"metadata": {
182+
"anaconda-cloud": {},
183+
"kernelspec": {
184+
"display_name": "Python 3",
185+
"language": "python",
186+
"name": "python3"
187+
},
188+
"language_info": {
189+
"codemirror_mode": {
190+
"name": "ipython",
191+
"version": 3
192+
},
193+
"file_extension": ".py",
194+
"mimetype": "text/x-python",
195+
"name": "python",
196+
"nbconvert_exporter": "python",
197+
"pygments_lexer": "ipython3",
198+
"version": "3.7.6"
199+
}
200+
},
201+
"nbformat": 4,
202+
"nbformat_minor": 1
203+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Hydrogen
2+
Helium
3+
Lithium
4+
Beryllium
5+
Boron
6+
Carbon
7+
Nitrogen
8+
Oxygen
9+
Fluorine
10+
Neon
11+
Sodium
12+
Magnesium
13+
Aluminum
14+
Silicon
15+
Phosphorus
16+
Sulfur
17+
Chlorine
18+
Argon
19+
Potassium
20+
Calcium

0 commit comments

Comments
 (0)