Last active
May 22, 2017 02:40
-
-
Save mGalarnyk/3a12115cdc9a50b4e0cea440f00d9e3a to your computer and use it in GitHub Desktop.
Python Elif Statement challenge for the blog post https://medium.com/@GalarnykMichael/python-basics-5-elif-statements-b8950dc71cf9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<b>Task</b>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"1. Assign num to an integer value. \n", | |
"2. Write a series of if, elif, else statements that will print the num you assigned. But for multiples of three print \"Fizz\" instead of the number and for the multiples of five print \"Buzz\". For numbers which are multiples of both three and five print \"FizzBuzz\"." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Solution 1\n", | |
"num = 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Buzz\n" | |
] | |
} | |
], | |
"source": [ | |
"if num % 3 == 0 and num % 5 == 0:\n", | |
" print('FizzBuzz')\n", | |
"elif num % 3 == 0:\n", | |
" print('Fizz')\n", | |
"elif num % 5 == 0:\n", | |
" print('Buzz')\n", | |
"else:\n", | |
" print(str(num))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Solution 2\n", | |
"num = 15" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"FizzBuzz\n" | |
] | |
} | |
], | |
"source": [ | |
"\"\"\"Notice how difficult this solution is relative to Solution 1\"\"\"\n", | |
"\n", | |
"string = \"\"\n", | |
"if num % 3 == 0:\n", | |
" string = string + \"Fizz\"\n", | |
"if num % 5 == 0:\n", | |
" string = string + \"Buzz\"\n", | |
"if num % 5 != 0 and num % 3 != 0:\n", | |
" string = string + str(num)\n", | |
"print(string)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"https://www.youtube.com/watch?v=NxBBBPjusyA" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda env:py36]", | |
"language": "python", | |
"name": "conda-env-py36-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment