-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cheuk_Joshua_Lab2_Exercises.ipnyb
1 lines (1 loc) · 7.37 KB
/
Cheuk_Joshua_Lab2_Exercises.ipnyb
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[{"file_id":"1xxQ2MPkrZN__j3ImT_TiGywhlICF-yck","timestamp":1706215234969}]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","source":["**Answer all exercise questions in a Jupyter Notebook file. You can use Markdown/Text for yes/no type questions!**\n","\n","\n","1. What type of data types are stored in variables below? (Use comments # to provide your answers, this is also reenforcing your use of comments in code cell; if any variable stores a data type you do not recognize, just state so) (2.5pts)\n","```\n","a = 100\n","b = \"coding\"\n","c = 10.0\n","d = True\n","e = coding\n","```\n"],"metadata":{"id":"yxQeTmK2cKCc"}},{"cell_type":"code","source":["#A = integer\n","#B= string\n","#C= float\n","#D= boolean\n","#E = not a data type"],"metadata":{"id":"2-w9sqFTV0Yp"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["2. Create a new variable called **my_character** and assign it your favorite tv character as a string. (2pts)\n","\n"," * Use print function to print out \"My favorite tv character is\" concatenated with your variable\n"," * Store that new string in a new variable of your choice\n"," * Use the len() function to determine how many characters are in that new string"],"metadata":{"id":"EMnQdDfvWl2U"}},{"cell_type":"code","source":["my_character = \"iron man\"\n","\n","print(\"My favorite TV character is \"+ my_character)\n","len(my_character)"],"metadata":{"id":"1UDEIMYUWGLw","executionInfo":{"status":"ok","timestamp":1706216394359,"user_tz":300,"elapsed":272,"user":{"displayName":"Joshua Cheuk","userId":"09800220979386665615"}},"outputId":"20373e9c-7aa8-43c5-813f-4af607e7829b","colab":{"base_uri":"https://localhost:8080/"}},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["My favorite TV character is iron man\n"]},{"output_type":"execute_result","data":{"text/plain":["8"]},"metadata":{},"execution_count":20}]},{"cell_type":"markdown","source":["\n","3. Use the input() function to take the user's answer to the question \"do you want to start a game? (yes or no)\" and store it in a variable called **choice**. (1.5pts)\n"," * Convert the input answer to all lowercase letters and store it in a new variable called **choice_lower**\n"," * Check if two variables are the same using a comparison operator and it should return a boolean output\n"],"metadata":{"id":"wYIfMM3QYuSY"}},{"cell_type":"code","source":["\n","choice = input(\"do you want to start a game? (yes or no)\")\n","\n","choice_lower = choice.lower()\n","\n","#check if the word is the same in terms whether or not the string is the same\n","\n","\n","choice_lower\n","choice_lower == choice"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"_7hmPBUCW1bk","executionInfo":{"status":"ok","timestamp":1706215650502,"user_tz":300,"elapsed":3611,"user":{"displayName":"Joshua Cheuk","userId":"09800220979386665615"}},"outputId":"97e5b35f-21a8-4bb2-fb0d-3676cfeb4d3c"},"execution_count":null,"outputs":[{"name":"stdout","output_type":"stream","text":["do you want to start a game? (yes or no)nO\n"]},{"output_type":"execute_result","data":{"text/plain":["False"]},"metadata":{},"execution_count":8}]},{"cell_type":"markdown","source":["4. Let's use the arithmetic operators we learned to solve some simple math problems. You can find the list of all operators on slide 26 in lab 2 slides. (1.5pts)\n"," * You have $100 budget to buy three gifts for your family memembers. The first gift costs $20, the second $30, and the last $27. Caculate how much money you will have left and use % (modulo operator) to determine whether the remaining money is an even or odd number. Use # (comment) to state the even/odd result and why "],"metadata":{"id":"mw__aguGYuWa"}},{"cell_type":"code","source":["gift_1 = 20\n","gift_2 = 30\n","gift_3 = 27\n","\n","change = 100-(gift_1 + gift_2 + gift_3)\n","\n","print(change)\n","change % 2\n","\n","#the result is an odd number because the result of the remaining money when using the modulo operator is 1"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"5yrAWUpOYNvc","executionInfo":{"status":"ok","timestamp":1706215798725,"user_tz":300,"elapsed":44,"user":{"displayName":"Joshua Cheuk","userId":"09800220979386665615"}},"outputId":"fb074aff-1996-47f8-a343-34937ad6e377"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["23\n"]},{"output_type":"execute_result","data":{"text/plain":["1"]},"metadata":{},"execution_count":10}]},{"cell_type":"markdown","source":["5. Let's write a program to check if a person's age is 21 years old or over. (2.5pts)\n"," * Use the input() function to take two inputs from the users: the current year and the birth year of the user and store both inputs in two separate variables\n"," * Calculate the age of the user by substracting the birth year from the current year and store it in a new variable\n"," * Compare the user's age with the integer 21 to determine if the user is 21 or older. The output should return a boolean result. (We learned \"==\" in class, and we can also use \">\", \"<\", \">=\", and \"<=\" for comparison, just like in math)\n"," * NOTE! When you use an input(), whatever the user gives you is considered a string by the computer. So 2024 as an input is considered \"2024\". You have to typecast it/do a data type conversion to turn a string into an integer so you can subtract it. That means you should use the int() function. It would look like this: **int(input(\" \"))** or **int(variable_name)**. Feel free to search online about how to use int()\n"],"metadata":{"id":"FA168K4Cosy8"}},{"cell_type":"code","source":["year= int(input(\"What year is it? \"))\n","user = int(input(\"What year were you born? \"))\n","\n","age = year - user\n","\n","21 <= age\n","\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"FcqO5-xLY9kh","executionInfo":{"status":"ok","timestamp":1706216813669,"user_tz":300,"elapsed":6532,"user":{"displayName":"Joshua Cheuk","userId":"09800220979386665615"}},"outputId":"5db64502-22c0-449f-db6f-936066efb5d6"},"execution_count":null,"outputs":[{"name":"stdout","output_type":"stream","text":["What year is it? 2020\n","What year were you born? 200\n"]},{"output_type":"execute_result","data":{"text/plain":["True"]},"metadata":{},"execution_count":24}]},{"cell_type":"markdown","source":["Bonus: Use string method .split() to split the string \"War is peace, freedom is slavery, ignorance is strength.\" into individual words as separate strings and print all individual strings out. You can find the whole list of string methods on slide 19 in lab 2 slides. Alternatively, you can simply search the usage of split() online. (2pts)"],"metadata":{"id":"OvuFJ8Q3e3T1"}},{"cell_type":"code","source":["quote = \"War is peace, freedom is slavery, ignorance is strength.\"\n","\n","quote.split()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"an7ELkdsaWKp","executionInfo":{"status":"ok","timestamp":1706216331549,"user_tz":300,"elapsed":24,"user":{"displayName":"Joshua Cheuk","userId":"09800220979386665615"}},"outputId":"01c24bf8-c544-421c-aa36-a7cebc662d0f"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['War',\n"," 'is',\n"," 'peace,',\n"," 'freedom',\n"," 'is',\n"," 'slavery,',\n"," 'ignorance',\n"," 'is',\n"," 'strength.']"]},"metadata":{},"execution_count":18}]}]}