|
83 | 83 | }, |
84 | 84 | { |
85 | 85 | "cell_type": "code", |
86 | | - "execution_count": 1, |
| 86 | + "execution_count": 7, |
87 | 87 | "metadata": { |
88 | 88 | "collapsed": true |
89 | 89 | }, |
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 | + ], |
91 | 105 | "source": [ |
92 | 106 | "# [] create Element_Quiz\n", |
93 | 107 | "\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", |
94 | 184 | "\n", |
95 | 185 | "\n" |
96 | 186 | ] |
|
120 | 210 | "name": "python", |
121 | 211 | "nbconvert_exporter": "python", |
122 | 212 | "pygments_lexer": "ipython3", |
123 | | - "version": "3.8.8" |
| 213 | + "version": "3.13.7" |
124 | 214 | } |
125 | 215 | }, |
126 | 216 | "nbformat": 4, |
|
0 commit comments