Skip to content

Commit 7b4d61c

Browse files
committed
2 parents 9fc4756 + bce2b74 commit 7b4d61c

14 files changed

+2537
-0
lines changed

Copy_of_07_Statements_Assessment_Test_checkpoint.ipynb

+454
Large diffs are not rendered by default.
+302
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"kernelspec": {
6+
"display_name": "Python 3",
7+
"language": "python",
8+
"name": "python3"
9+
},
10+
"language_info": {
11+
"codemirror_mode": {
12+
"name": "ipython",
13+
"version": 3
14+
},
15+
"file_extension": ".py",
16+
"mimetype": "text/x-python",
17+
"name": "python",
18+
"nbconvert_exporter": "python",
19+
"pygments_lexer": "ipython3",
20+
"version": "3.6.6"
21+
},
22+
"colab": {
23+
"name": "Copy of 09-Guessing Game Challenge.ipynb",
24+
"provenance": [],
25+
"include_colab_link": true
26+
}
27+
},
28+
"cells": [
29+
{
30+
"cell_type": "markdown",
31+
"metadata": {
32+
"id": "view-in-github",
33+
"colab_type": "text"
34+
},
35+
"source": [
36+
"<a href=\"https://colab.research.google.com/github/DebjitHore/Python-Codes/blob/master/Copy_of_09_Guessing_Game_Challenge.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"metadata": {
42+
"id": "dFC0TdDrtrIX"
43+
},
44+
"source": [
45+
"___\n",
46+
"\n",
47+
"<a href='https://www.udemy.com/user/joseportilla/'><img src='https://github.com/Pierian-Data/Complete-Python-3-Bootcamp/blob/master/Pierian_Data_Logo.png?raw=1'/></a>\n",
48+
"___\n",
49+
"<center><em>Content Copyright by Pierian Data</em></center>"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {
55+
"id": "-WR_u_GUtrIb"
56+
},
57+
"source": [
58+
"# Guessing Game Challenge\n",
59+
"\n",
60+
"Let's use `while` loops to create a guessing game.\n",
61+
"\n",
62+
"The Challenge:\n",
63+
"\n",
64+
"Write a program that picks a random integer from 1 to 100, and has players guess the number. The rules are:\n",
65+
"\n",
66+
"1. If a player's guess is less than 1 or greater than 100, say \"OUT OF BOUNDS\"\n",
67+
"2. On a player's first turn, if their guess is\n",
68+
" * within 10 of the number, return \"WARM!\"\n",
69+
" * further than 10 away from the number, return \"COLD!\"\n",
70+
"3. On all subsequent turns, if a guess is \n",
71+
" * closer to the number than the previous guess return \"WARMER!\"\n",
72+
" * farther from the number than the previous guess, return \"COLDER!\"\n",
73+
"4. When the player's guess equals the number, tell them they've guessed correctly *and* how many guesses it took!\n",
74+
"\n",
75+
"You can try this from scratch, or follow the steps outlined below. A separate Solution notebook has been provided. Good luck!\n"
76+
]
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {
81+
"id": "5q_uW-4MtrIc"
82+
},
83+
"source": [
84+
"#### First, pick a random integer from 1 to 100 using the random module and assign it to a variable\n",
85+
"\n",
86+
"Note: `random.randint(a,b)` returns a random integer in range `[a, b]`, including both end points."
87+
]
88+
},
89+
{
90+
"cell_type": "code",
91+
"metadata": {
92+
"collapsed": true,
93+
"id": "cfHZq3YOtrIc"
94+
},
95+
"source": [
96+
"import random\n",
97+
"x_guess= random.randint(0,100)"
98+
],
99+
"execution_count": null,
100+
"outputs": []
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {
105+
"id": "hTVAFRM1trId"
106+
},
107+
"source": [
108+
"#### Next, print an introduction to the game and explain the rules"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"metadata": {
114+
"collapsed": true,
115+
"id": "p5sErCDStrIe",
116+
"colab": {
117+
"base_uri": "https://localhost:8080/"
118+
},
119+
"outputId": "51dae842-c75c-48e7-e43d-f082c326feeb"
120+
},
121+
"source": [
122+
"print(\"WELCOME TO GUESS ME!\")\n",
123+
"print(\"I'm thinking of a number between 1 and 100\")\n",
124+
"print(\"If your guess is more than 10 away from my number, I'll tell you you're COLD\")\n",
125+
"print(\"If your guess is within 10 of my number, I'll tell you you're WARM\")\n",
126+
"print(\"If your guess is farther than your most recent guess, I'll say you're getting COLDER\")\n",
127+
"print(\"If your guess is closer than your most recent guess, I'll say you're getting WARMER\")\n",
128+
"print(\"LET'S PLAY!\")"
129+
],
130+
"execution_count": null,
131+
"outputs": [
132+
{
133+
"output_type": "stream",
134+
"text": [
135+
"WELCOME TO GUESS ME!\n",
136+
"I'm thinking of a number between 1 and 100\n",
137+
"If your guess is more than 10 away from my number, I'll tell you you're COLD\n",
138+
"If your guess is within 10 of my number, I'll tell you you're WARM\n",
139+
"If your guess is farther than your most recent guess, I'll say you're getting COLDER\n",
140+
"If your guess is closer than your most recent guess, I'll say you're getting WARMER\n",
141+
"LET'S PLAY!\n"
142+
],
143+
"name": "stdout"
144+
}
145+
]
146+
},
147+
{
148+
"cell_type": "markdown",
149+
"metadata": {
150+
"id": "FnWlbDtWtrIf"
151+
},
152+
"source": [
153+
"#### Create a list to store guesses\n",
154+
"\n",
155+
"Hint: zero is a good placeholder value. It's useful because it evaluates to \"False\""
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"metadata": {
161+
"collapsed": true,
162+
"id": "IaVgm2QJtrIg"
163+
},
164+
"source": [
165+
"\n",
166+
"guesses= [0]"
167+
],
168+
"execution_count": null,
169+
"outputs": []
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"metadata": {
174+
"id": "V0KIhjx-trIh"
175+
},
176+
"source": [
177+
"#### Write a `while` loop that asks for a valid guess. Test it a few times to make sure it works."
178+
]
179+
},
180+
{
181+
"cell_type": "code",
182+
"metadata": {
183+
"collapsed": true,
184+
"id": "-p9p99TftrIi",
185+
"colab": {
186+
"base_uri": "https://localhost:8080/"
187+
},
188+
"outputId": "e0b6bae2-d5d5-4b63-c097-1983563e01bb"
189+
},
190+
"source": [
191+
"while True:\n",
192+
" guess= input('Enter a number of your choice')\n",
193+
" guess= int(guess)\n",
194+
" if (guess >100 and guess <1):\n",
195+
" print ('OUT OF BOUNDS')\n",
196+
" continue\n",
197+
" \n",
198+
" if (guess == x_guess):\n",
199+
" l= len(guesses)\n",
200+
" print('You are successful')\n",
201+
" print('The number of guesses it took you is {}'.format(l))\n",
202+
" break\n",
203+
"\n",
204+
" guesses.append(guess)\n",
205+
"\n",
206+
" if guesses[-2]:\n",
207+
" if abs(guess-x_guess)< abs(guess-guesses[-2]):\n",
208+
" print ('WARMER')\n",
209+
" else:\n",
210+
" print('COLDER') \n",
211+
" else:\n",
212+
" if abs(x_guess-guess) <= 10:\n",
213+
" print('WARM!')\n",
214+
" else:\n",
215+
" print('COLD!') \n",
216+
" "
217+
],
218+
"execution_count": null,
219+
"outputs": [
220+
{
221+
"output_type": "stream",
222+
"text": [
223+
"Enter a number of your choice45\n",
224+
"COLD!\n",
225+
"Enter a number of your choice40\n",
226+
"COLDER\n",
227+
"Enter a number of your choice39\n",
228+
"COLDER\n",
229+
"Enter a number of your choice47\n",
230+
"COLDER\n",
231+
"Enter a number of your choice53\n",
232+
"COLDER\n",
233+
"Enter a number of your choice22\n",
234+
"WARMER\n",
235+
"Enter a number of your choice20\n",
236+
"COLDER\n",
237+
"Enter a number of your choice21\n",
238+
"COLDER\n",
239+
"Enter a number of your choice24\n",
240+
"WARMER\n",
241+
"Enter a number of your choice25\n",
242+
"COLDER\n",
243+
"Enter a number of your choice23\n",
244+
"You are successful\n",
245+
"The number of guesses it took you is 11\n"
246+
],
247+
"name": "stdout"
248+
}
249+
]
250+
},
251+
{
252+
"cell_type": "markdown",
253+
"metadata": {
254+
"id": "ztcaTsf3trIj"
255+
},
256+
"source": [
257+
"#### Write a `while` loop that compares the player's guess to our number. If the player guesses correctly, break from the loop. Otherwise, tell the player if they're warmer or colder, and continue asking for guesses.\n",
258+
"\n",
259+
"Some hints:\n",
260+
"* it may help to sketch out all possible combinations on paper first!\n",
261+
"* you can use the `abs()` function to find the positive difference between two numbers\n",
262+
"* if you append all new guesses to the list, then the previous guess is given as `guesses[-2]`"
263+
]
264+
},
265+
{
266+
"cell_type": "code",
267+
"metadata": {
268+
"collapsed": true,
269+
"id": "HL77f27strIk"
270+
},
271+
"source": [
272+
"while True:\n",
273+
"\n",
274+
" # we can copy the code from above to take an input\n",
275+
"\n",
276+
" pass"
277+
],
278+
"execution_count": null,
279+
"outputs": []
280+
},
281+
{
282+
"cell_type": "markdown",
283+
"metadata": {
284+
"id": "3_Mu35R8trIl"
285+
},
286+
"source": [
287+
"That's it! You've just programmed your first game!\n",
288+
"\n",
289+
"In the next section we'll learn how to turn some of these repetitive actions into *functions* that can be called whenever we need them."
290+
]
291+
},
292+
{
293+
"cell_type": "markdown",
294+
"metadata": {
295+
"id": "3T3TMK_1trIl"
296+
},
297+
"source": [
298+
"### Good Job!"
299+
]
300+
}
301+
]
302+
}

0 commit comments

Comments
 (0)