diff --git a/cfu-data-types.ipynb b/cfu-data-types.ipynb index e0fee02..e115413 100644 --- a/cfu-data-types.ipynb +++ b/cfu-data-types.ipynb @@ -49,11 +49,107 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your name is: Susanna\n", + "Your age is: 34\n", + "Your address is: Travessera\n", + "Your salary is: 2300\n", + "Your expenses is: 1500\n", + "You have 800.0 left after expenses.\n" + ] + } + ], + "source": [ + "#Prompt the user to enter their personal information such as name, age, address, salary and expenses.\n", + "list_of_info = [\"name\", \"age\", \"address\", \"salary\", \"expenses\"]\n", + "for info in list_of_info:\n", + " user_input = input(f\"Please enter your {info}: \")\n", + " print(f\"Your {info} is: {user_input}\")\n", + "\n", + "#Perform some calculations to determine how much money the user has left after expenses.\n", + "salary = float(input(\"Please enter your salary: \"))\n", + "expenses = float(input(\"Please enter your expenses: \"))\n", + "if salary < 0 or expenses < 0:\n", + " print(\"Salary and expenses must be non-negative numbers.\")\n", + " exit(1)\n", + "remaining_money = salary - expenses\n", + "print(f\"You have {remaining_money} left after expenses.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "At the age of 34, you have 800.0 left after expenses.\n" + ] + } + ], + "source": [ + "#Use data type casting to ensure that the age, salary, and expenses are stored as integers or floats, as appropriate. Round the remaining money to one decimal place.\n", + "age = int(input(\"Please enter your age: \"))\n", + "salary = float(input(\"Please enter your salary: \"))\n", + "expenses = float(input(\"Please enter your expenses: \"))\n", + "remaining_money = round(salary - expenses, 1)\n", + "print(f\"At the age of {age}, you have {remaining_money} left after expenses.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Is your remaining money sufficient (>= 500)? True\n" + ] + } + ], "source": [ - "# Your code here\n" + "#Use a boolean variable to indicate whether the remaining salary is greater than or equal to 500.\n", + "is_sufficient = remaining_money >= 500\n", + "print(f\"Is your remaining money sufficient (>= 500)? {is_sufficient}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Susanna, who is 34 years old, and lives in Travessera has 800.0 dollars left from her salary after expenses. It is True that she has more than $500 left.\n" + ] + } + ], + "source": [ + "#Finally, you will use string formatting to print a message to the screen in the following format: \n", + "#\"*name*, who is *age* years old, and lives in *address* has *remaining_salary* dollars left from her salary after expenses. \n", + "# It is *is_salary_good* that she has more than $500 left.\", using the mentioned variables.\n", + "\n", + "user_input = {'name': input(\"Please enter your name: \"),\n", + " 'age': age,\n", + " 'address': input(\"Please enter your address: \")}\n", + "remaining_money = round(salary - expenses, 1)\n", + "is_sufficient = remaining_money > 500\n", + "\n", + "print(f\"{user_input['name']}, who is {age} years old, and lives in {user_input['address']} has {remaining_money} dollars left from her salary after expenses. \"\n", + " f\"It is {is_sufficient} that she has more than $500 left.\")\n", + "\n" ] }, { @@ -85,7 +181,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -102,17 +198,114 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "some say the world will end in fire\n", + "some say in ice\n", + "from what i’ve tasted of desire\n", + "i hold with those who favor fire\n", + "but if it had to perish twice\n", + "i think i know enough of hate\n", + "to say that for destruction ice\n", + "is also great\n", + "and would suffice\n" + ] + } + ], + "source": [ + "#Write code that takes text from the variable 'poem', removes the punctuation and leaves everything in lowercase.\n", + "poem = \"\"\"Some say the world will end in fire,\n", + "Some say in ice.\n", + "From what I’ve tasted of desire\n", + "I hold with those who favor fire.\n", + "But if it had to perish twice,\n", + "I think I know enough of hate\n", + "To say that for destruction ice\n", + "Is also great\n", + "And would suffice.\"\"\"\n", + "\n", + "new_poem = poem.lower()\n", + "for char in \".,'\\\"!?;:\":\n", + " new_poem = new_poem.replace(char, \"\")\n", + "\n", + "#Print the new poem.\n", + "print(new_poem)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "some say the world will end in fire\n", + "some say in ice\n", + "from what i’ve tasted of desire\n", + "i hold with those who favor fire\n", + "but if it had to perish twice\n", + "i think i know enough of hate\n", + "to say that for destruction ice\n", + "is also great\n", + "and would suffice python is awesome!\n" + ] + } + ], + "source": [ + "#Concatenate the resulting string with the string \"python is awesome!\" and store the result in a new varible.\n", + "new_poem += \" python is awesome!\"\n", + "#Print the new variable.\n", + "print(new_poem)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The length of the new poem is: 259 characters.\n" + ] + } + ], + "source": [ + "#Print the length of the new string.\n", + "print(f\"The length of the new poem is: {len(new_poem)} characters.\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['some', 'say', 'the', 'world', 'will', 'end', 'in', 'fire\\nsome', 'say', 'in', 'ice\\nfrom', 'what', 'i’ve', 'tasted', 'of', 'desire\\ni', 'hold', 'with', 'those', 'who', 'favor', 'fire\\nbut', 'if', 'it', 'had', 'to', 'perish', 'twice\\ni', 'think', 'i', 'know', 'enough', 'of', 'hate\\nto', 'say', 'that', 'for', 'destruction', 'ice\\nis', 'also', 'great\\nand', 'would', 'suffice', 'python', 'is', 'awesome!']\n" + ] + } + ], "source": [ - "# Your code here\n" + "#Split the string into a list of strings using the space delimiter, save it in a variable `poem_list` and print it. \n", + "poem_list = new_poem.split(\" \")\n", + "print(poem_list)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -126,7 +319,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4,