Skip to content

Commit 2f2b55c

Browse files
committed
P2M5 Final code work
1 parent a7722db commit 2f2b55c

4 files changed

Lines changed: 225 additions & 3 deletions

File tree

P2 Python Fundamentals/Module_5.0_Required_FINAL_Project_Python_Fundamentals.ipynb

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,104 @@
8383
},
8484
{
8585
"cell_type": "code",
86-
"execution_count": 1,
86+
"execution_count": 7,
8787
"metadata": {
8888
"collapsed": true
8989
},
90-
"outputs": [],
90+
"outputs": [
91+
{
92+
"name": "stdout",
93+
"output_type": "stream",
94+
"text": [
95+
"Welcome Ben Reed, list any 5 of the first 20 elements in the Periodic Table: \n",
96+
"\n",
97+
"==============================\n",
98+
"60 % correct\n",
99+
"Found: Hydrogen Lithium Sodium\n",
100+
"Not Found: Berries Zenon\n",
101+
"==============================\n"
102+
]
103+
}
104+
],
91105
"source": [
92106
"# [] create Element_Quiz\n",
93107
"\n",
108+
"##Download the data file for elements1_20.txt\n",
109+
"!curl -s -O https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/elements1_20.txt\n",
110+
"\n",
111+
"def get_names(): ## creates a function that gets the person's name, and gets input for the name of the element.\n",
112+
" ## If the input is empty, it says space not allowed.\n",
113+
" ## if it is a duplicate, it says no duplicates.\n",
114+
" ## If it is a new input, it appemd it to the input_names list.\n",
115+
"\n",
116+
" input_names = []\n",
117+
"\n",
118+
" while len(input_names) < 5:\n",
119+
" name = input(\"Enter the name of an element: \").lower().strip()\n",
120+
"\n",
121+
" if name == \"\":\n",
122+
" print(\"Empty Space Not Allowed\")\n",
123+
"\n",
124+
" elif name in input_names:\n",
125+
"\n",
126+
" print(f\"{name} was already entered <--- no duplicates allowed\")\n",
127+
"\n",
128+
" else:\n",
129+
" input_names.append(name)\n",
130+
"\n",
131+
" return input_names\n",
132+
"\n",
133+
"## The program welcomes the user and asks for 5 elements.\n",
134+
"your_name = input(\"Please enter your full name: \")\n",
135+
"print(f\"Welcome {your_name}, list any 5 of the first 20 elements in the Periodic Table: \")\n",
136+
"\n",
137+
"first_20_elements = []\n",
138+
"\n",
139+
"try: ## The program opens elements1_20.txt and reads one line at a time.\n",
140+
" ## The program also removes any whitespace, makes it lowercase, and appends to the first_20_elements list.\n",
141+
"\n",
142+
" elements_file = open('elements1_20.txt', 'r')\n",
143+
"\n",
144+
" for line in elements_file:\n",
145+
"\n",
146+
" first_20_elements.append(line.strip().lower())\n",
147+
"\n",
148+
" elements_file.close()\n",
149+
"\n",
150+
"except FileNotFoundError: ## The code will exit the program and notify of the elements1_20.txt file was not found.\n",
151+
" print(\"Error: The file 'elements1_20.txt' was not found.\")\n",
152+
" exit()\n",
153+
"\n",
154+
"quiz_reponses = get_names() ## Call the get_names function.\n",
155+
"correct_responses = [] ## Makes a list of correct responses.\n",
156+
"incorrect_responses = [] ## Makes a list of incorrect repsonses.\n",
157+
"\n",
158+
"for response in quiz_reponses: ## Now we iterate through the 5 reponses from the user input.\n",
159+
" ## It comparess each response to the list of 20 elements.\n",
160+
" if response in first_20_elements:\n",
161+
" ## If correct, the input is appended to the correct_response list.\n",
162+
" correct_responses.append(response)\n",
163+
"\n",
164+
" else: ## if the input is incorrect, it appends it to the incorrect_response list.\n",
165+
" incorrect_responses.append(response)\n",
166+
"\n",
167+
"percent_correct = len(correct_responses) * 20 ## the list of correct_reponse times 20 gives the percent correct.\n",
168+
"percent_correct = int(percent_correct) ##turns percent correct into an int.\n",
169+
"\n",
170+
"print(\"\\n\" + \"=\"*30)\n",
171+
"print(f\"{percent_correct} % correct\")\n",
172+
"\n",
173+
"found_list = [name.title() for name in correct_responses] ## turns correct responses into Title form for printing.\n",
174+
"\n",
175+
"print(\"Found: \", end=\"\")\n",
176+
"print(' '.join(found_list)) ## Joins the found_list into a single string with spaces betweem.\n",
177+
"\n",
178+
"not_found_list = [name.title() for name in incorrect_responses]## turns incorrect responses into Title form for printing.\n",
179+
"print(\"Not Found: \", end=\"\")\n",
180+
"print(\" \".join(not_found_list)) ## Joins the found_list into a single string with spaces betweem.\n",
181+
"\n",
182+
"print(\"=\"*30)\n",
183+
"\n",
94184
"\n",
95185
"\n"
96186
]
@@ -120,7 +210,7 @@
120210
"name": "python",
121211
"nbconvert_exporter": "python",
122212
"pygments_lexer": "ipython3",
123-
"version": "3.8.8"
213+
"version": "3.13.7"
124214
}
125215
},
126216
"nbformat": 4,
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

P2M5BenjaminReed.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [] create Element_Quiz
2+
3+
##Download the data file for elements1_20.txt
4+
import os
5+
6+
os.system ("curl -s -O https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/elements1_20.txt")
7+
8+
def get_names(): ## creates a function that gets the person's name, and gets input for the name of the element.
9+
## If the input is empty, it says space not allowed.
10+
## if it is a duplicate, it says no duplicates.
11+
## If it is a new input, it appemd it to the input_names list.
12+
13+
input_names = []
14+
15+
while len(input_names) < 5:
16+
name = input("Enter the name of an element: ").lower().strip()
17+
18+
if name == "":
19+
print("Empty Space Not Allowed")
20+
21+
elif name in input_names:
22+
23+
print(f"{name} was already entered <--- no duplicates allowed")
24+
25+
else:
26+
input_names.append(name)
27+
28+
return input_names
29+
30+
## The program welcomes the user and asks for 5 elements.
31+
your_name = input("Please enter your full name: ")
32+
print(f"Welcome {your_name}, list any 5 of the first 20 elements in the Periodic Table: ")
33+
34+
first_20_elements = []
35+
36+
try: ## The program opens elements1_20.txt and reads one line at a time.
37+
## The program also removes any whitespace, makes it lowercase, and appends to the first_20_elements list.
38+
39+
elements_file = open('elements1_20.txt', 'r')
40+
41+
for line in elements_file:
42+
43+
first_20_elements.append(line.strip().lower())
44+
45+
elements_file.close()
46+
47+
except FileNotFoundError: ## The code will exit the program and notify of the elements1_20.txt file was not found.
48+
print("Error: The file 'elements1_20.txt' was not found.")
49+
exit()
50+
51+
quiz_reponses = get_names() ## Call the get_names function.
52+
correct_responses = [] ## Makes a list of correct responses.
53+
incorrect_responses = [] ## Makes a list of incorrect repsonses.
54+
55+
for response in quiz_reponses: ## Now we iterate through the 5 reponses from the user input.
56+
## It comparess each response to the list of 20 elements.
57+
if response in first_20_elements:
58+
## If correct, the input is appended to the correct_response list.
59+
correct_responses.append(response)
60+
61+
else: ## if the input is incorrect, it appends it to the incorrect_response list.
62+
incorrect_responses.append(response)
63+
64+
percent_correct = len(correct_responses) * 20 ## the list of correct_reponse times 20 gives the percent correct.
65+
percent_correct = int(percent_correct) ##turns percent correct into an int.
66+
67+
BOLD = '\033[1m' ## makes text bold
68+
RED = '\033[91m' ## Specifies the color red.
69+
ENDC = '\033[0m' ## Specifies the normal color.
70+
B_GREEN = '\033[42m'
71+
72+
if percent_correct < 70: ##If the user scores less than 70%, the putput is red.
73+
COLOR = RED
74+
75+
else:
76+
COLOR = ENDC
77+
78+
print("\n" + "<>"*30)
79+
print()
80+
print(f"{COLOR}{BOLD}{percent_correct} % correct{ENDC}")
81+
82+
found_list = [f"{B_GREEN}{BOLD}{name.title()}{ENDC}" for name in correct_responses] ## turns correct responses into Title form for printing.
83+
84+
print("Found: ", end="")
85+
print(', '.join(found_list)) ## Joins the found_list into a single string with spaces betweem.
86+
87+
not_found_list = [name.title() for name in incorrect_responses]## turns incorrect responses into Title form for printing.
88+
print("Not Found: ", end="")
89+
print(", ".join(not_found_list)) ## Joins the found_list into a single string with spaces betweem.
90+
91+
print()
92+
print("<>"*30)

elements1_20.txt

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)