Created
May 30, 2018 08:19
-
-
Save samueljackson92/ee1090635490eccfc6b7dd08533a23d7 to your computer and use it in GitHub Desktop.
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": [ | |
"### Exercise 2\n", | |
"\n", | |
"Type in the following code and run it. What is the answer?\n", | |
"```python\n", | |
"a = 15 + 5 \n", | |
"b = 20 - a \n", | |
"c = 15 / 5 \n", | |
"d = 16 + c \n", | |
"ans = a + b + c + d\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"42" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"a = 15 + 5 \n", | |
"b = 20 - a \n", | |
"c = 15 / 5 \n", | |
"d = 16 + c \n", | |
"ans = a + b + c + d\n", | |
"\n", | |
"ans" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercise 3\n", | |
"\n", | |
"Accept some user input and print it out" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Please enter your favourite color:\n", | |
"Your fav color is: hello\n" | |
] | |
} | |
], | |
"source": [ | |
"print (\"Please enter your favourite color:\")\n", | |
"color = input()\n", | |
"print (\"Your fav color is: \" + color)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercise 4\n", | |
"\n", | |
"Roll the dice. Add a simulated dice roll to the program." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"4\n" | |
] | |
} | |
], | |
"source": [ | |
"import random\n", | |
"size = input() \n", | |
"roll = random.randint(1,int(size))\n", | |
"print (roll)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercise 5\n", | |
"\n", | |
"Print try the following statements. What is the output?\n", | |
"\n", | |
"```python\n", | |
"a = 5 \n", | |
"b = 10 \n", | |
"a > b \n", | |
"a < b\n", | |
"\n", | |
"a+b == b+a \n", | |
"a-b == b-a \n", | |
"a*b == b*a \n", | |
"a/b != b/a \n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a > b: False\n", | |
"a < b: True\n", | |
"a+b == b+a: True\n", | |
"a-b == b-a: False\n", | |
"a*b == b*a: True\n", | |
"a/b != b/a: True\n" | |
] | |
} | |
], | |
"source": [ | |
"a = 5 \n", | |
"b = 10 \n", | |
"\n", | |
"statement1 = a > b\n", | |
"statement2 = a < b\n", | |
"statement3 = a+b == b+a\n", | |
"statement4 = a-b == b-a\n", | |
"statement5 = a*b == b*a\n", | |
"statement6 = a/b != b/a\n", | |
"\n", | |
"print (\"a > b: {}\".format(statement1))\n", | |
"print (\"a < b: {}\".format(statement2))\n", | |
"print (\"a+b == b+a: {}\".format(statement3))\n", | |
"print (\"a-b == b-a: {}\".format(statement4))\n", | |
"print (\"a*b == b*a: {}\".format(statement5))\n", | |
"print (\"a/b != b/a: {}\".format(statement6))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercise 6\n", | |
"\n", | |
"Guess the numbers game." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"You guessed correct!\n", | |
"Player 2 wins!\n" | |
] | |
} | |
], | |
"source": [ | |
"number_to_guess = random.randint(1, 11)\n", | |
"\n", | |
"while True:\n", | |
" print (\"Player 1 enter a number to guess:\")\n", | |
" new_guess = input()\n", | |
" \n", | |
" if new_guess == number_to_guess:\n", | |
" print(\"You guessed correct!\")\n", | |
" player1_wins = True\n", | |
" break\n", | |
" else:\n", | |
" print(\"That's not the number I was thinking of!\")\n", | |
" print(\"Try again!\")\n", | |
" \n", | |
" print (\"Player 2 enter a number to guess:\")\n", | |
" new_guess = input()\n", | |
" \n", | |
" if new_guess == number_to_guess:\n", | |
" print(\"You guessed correct!\")\n", | |
" player_1_wins = False\n", | |
" break\n", | |
" else:\n", | |
" print(\"That's not the number I was thinking of!\")\n", | |
" print(\"Try again!\")\n", | |
" \n", | |
"if player_1_wins:\n", | |
" print (\"Player 1 wins!\")\n", | |
"else:\n", | |
" print (\"Player 2 wins!\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercise 7\n", | |
"\n", | |
"Functions for guess the numbers game" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 1 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 2 enter a number to guess:\n", | |
"That's not the number I was thinking of!\n", | |
"Try again!\n", | |
"Player 1 enter a number to guess:\n", | |
"You guessed correct!\n", | |
"Player 1 wins!\n" | |
] | |
} | |
], | |
"source": [ | |
"import random\n", | |
"number_to_guess = random.randint(1, 11)\n", | |
"\n", | |
"def get_guess_for_player(name):\n", | |
" print (name + \" enter a number to guess:\")\n", | |
" return input()\n", | |
" \n", | |
"def check_guess(new_guess):\n", | |
" if new_guess == number_to_guess:\n", | |
" print(\"You guessed correct!\")\n", | |
" return True\n", | |
" else:\n", | |
" print(\"That's not the number I was thinking of!\")\n", | |
" print(\"Try again!\")\n", | |
" return False\n", | |
"\n", | |
"while True:\n", | |
" new_guess = get_guess_for_player(\"Player 1\")\n", | |
" is_correct = check_guess(new_guess)\n", | |
" \n", | |
" if is_correct:\n", | |
" player_1_wins = True\n", | |
" break\n", | |
" \n", | |
" new_guess = get_guess_for_player(\"Player 2\")\n", | |
" is_correct = check_guess(new_guess)\n", | |
"\n", | |
" if is_correct:\n", | |
" player_1_wins = False\n", | |
" break\n", | |
" \n", | |
"if player_1_wins:\n", | |
" print (\"Player 1 wins!\")\n", | |
"else:\n", | |
" print (\"Player 2 wins!\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercise 8\n", | |
"\n", | |
"Program to check if a given number is a prime number" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Enter a number:\n", | |
"2\n", | |
"10 is not prime!\n" | |
] | |
} | |
], | |
"source": [ | |
"print (\"Enter a number:\")\n", | |
"number = input()\n", | |
"\n", | |
"not_prime = False\n", | |
"count = 2\n", | |
"while count < number:\n", | |
" \n", | |
" if number % count == 0:\n", | |
" print count\n", | |
" not_prime = True\n", | |
" break\n", | |
" \n", | |
" count += 1\n", | |
"\n", | |
"if number < 2:\n", | |
" not_prime = False\n", | |
" \n", | |
"if not_prime:\n", | |
" print (str(number) + \" is not prime!\")\n", | |
"else:\n", | |
" print (str(number) + \" is prime!\")\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Exercise 9\n", | |
"\n", | |
"Hangman" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 55, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"How many guesses?\n", | |
"Enter the word:\n", | |
"Current Guess: 1\n", | |
"Guesses left: 2\n", | |
"Enter your guess:\n", | |
"Incorrect! Try Again!\n", | |
"Current Guess: 2\n", | |
"Guesses left: 1\n", | |
"Enter your guess:\n", | |
"Correct! you win!\n" | |
] | |
} | |
], | |
"source": [ | |
"print (\"How many guesses?\")\n", | |
"number_of_guesses = input()\n", | |
"print (\"Enter the word:\")\n", | |
"word = input()\n", | |
"\n", | |
"\n", | |
"current_guess = 0\n", | |
"while current_guess < number_of_guesses:\n", | |
" current_guess += 1\n", | |
" print (\"Current Guess: \" + str(current_guess))\n", | |
" print (\"Guesses left: \" + str(number_of_guesses+1 - current_guess))\n", | |
"\n", | |
" print (\"Enter your guess:\")\n", | |
" guess = input()\n", | |
" \n", | |
" if guess == word:\n", | |
" print (\"Correct! you win!\")\n", | |
" break\n", | |
" else:\n", | |
" print (\"Incorrect! Try Again!\")\n", | |
" \n", | |
" " | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment