Skip to content

Commit dc7b414

Browse files
4/6/25 test yourself on func (copy and paste)
1 parent bf0047b commit dc7b414

4 files changed

Lines changed: 176 additions & 40 deletions

File tree

lessons/02_Loops/10_More_Iterables.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@
249249
},
250250
{
251251
"cell_type": "code",
252-
"execution_count": 6,
252+
"execution_count": 1,
253253
"metadata": {},
254254
"outputs": [
255255
{
@@ -329,7 +329,7 @@
329329
},
330330
{
331331
"cell_type": "code",
332-
"execution_count": 8,
332+
"execution_count": 2,
333333
"metadata": {},
334334
"outputs": [
335335
{

lessons/02_Loops/11_Iterable_Turtle.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 2,
5+
"execution_count": 1,
66
"metadata": {},
77
"outputs": [
88
{
@@ -11,7 +11,7 @@
1111
"<function turtle.exitonclick()>"
1212
]
1313
},
14-
"execution_count": 2,
14+
"execution_count": 1,
1515
"metadata": {},
1616
"output_type": "execute_result"
1717
}

lessons/02_Loops/13_Number_Guess.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,41 @@
3232

3333
import random
3434
n = random.randint(1, 100)
35+
while n % 7 == 0:
36+
n = random.randint(1, 100)
3537
def ask_integer(prompt):
3638
while True:
3739
try:
3840
return int(input(prompt))
3941
except ValueError:
4042
print("Please enter a valid number!")
4143

44+
4245
while True:
43-
guess = ask_integer("Guess a number between 1 and 100: ")
44-
if guess <= n:
45-
print("Too low, guess again!")
46-
elif guess % 7 == 0:
47-
print("That is a very bad number, starting over")
48-
elif guess >= n:
49-
print("Too high, guess again!")
46+
guess = ask_integer("Guess a number between 1 and 100: ")
47+
if guess % 7 == 0:
48+
print("Start over")
49+
n = random.randint(1, 100)
50+
while n % 7 == 0:
51+
n = random.randint(1, 100)
52+
elif guess > n:
53+
print("Too high")
54+
elif guess < n:
55+
print("Too low")
5056
elif guess == n:
51-
print("Correct, good job. Ending game!")
57+
print("Correct, breaking program")
5258
break
5359

54-
# marked for review ask teacher oncampus
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
5570
# Pick the random number
5671

5772
# In your loop:

lessons/03_Data_Structures_Func/01_Functions.ipynb

Lines changed: 148 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,29 @@
246246
},
247247
{
248248
"cell_type": "code",
249-
"execution_count": null,
249+
"execution_count": 12,
250250
"metadata": {},
251251
"outputs": [],
252252
"source": [
253253
"# Test yourself\n",
254254
"\n",
255255
"# Write your own find_char function that takes a character and a string\n",
256+
"def find_char(character, statement):\n",
257+
" for i in range(len(statement)):\n",
258+
" statement[i]\n",
259+
" if character == statement[i]:\n",
260+
" return i\n",
261+
" \n",
262+
" return -1\n",
263+
" \n",
264+
"\n",
265+
"\n",
256266
"\n",
257267
"assert find_char('x', 'My fox likes bricks') == 5\n",
258268
"assert find_char('z', 'There is a zebra in the garden') == 11\n",
259269
"assert find_char('w', \"I've lost my shoes\") == -1\n",
260270
"\n",
261-
"find_char('')"
271+
"assert find_char('y', \"Sundaay\") == 6"
262272
]
263273
},
264274
{
@@ -300,24 +310,48 @@
300310
},
301311
{
302312
"cell_type": "code",
303-
"execution_count": null,
313+
"execution_count": 24,
304314
"metadata": {},
305-
"outputs": [],
315+
"outputs": [
316+
{
317+
"name": "stdout",
318+
"output_type": "stream",
319+
"text": [
320+
"3\n",
321+
"1\n",
322+
"Different\n",
323+
"Same\n",
324+
"4\n",
325+
"6\n",
326+
"Different\n"
327+
]
328+
}
329+
],
306330
"source": [
307331
"# Test Yourself\n",
308332
"\n",
309333
"# Write a function to add two numbers and return the result\n",
310-
"\n",
334+
"def add(number1, number2):\n",
335+
" return number1 + number2\n",
336+
"print(add(1, 2))\n",
311337
"# Write a function to subtract two numbers and return the result \n",
312-
"\n",
338+
"def subtract(number3, number4):\n",
339+
" return number3 - number4\n",
340+
"print(subtract(4, 3))\n",
313341
"# Write a function to print \"Same\" if two numbers are the same, otherwise print \"Different\"\n",
314-
"\n",
342+
"def same(number5, number6):\n",
343+
" if number5 == number6:\n",
344+
" return \"Same\"\n",
345+
" else:\n",
346+
" return \"Different\"\n",
347+
"print(same(5, 6))\n",
348+
"print(same(5, 5))\n",
315349
"# Use your functions to show that 2 + 2 = 4\n",
316-
"\n",
350+
"print(add(2, 2))\n",
317351
"# Use your functions to show that ( 5 + 3) - 2 = 6\n",
318-
"\n",
352+
"print(subtract(add(5, 3), 2))\n",
319353
"# Use your functions to show that 2 + 2 != 3 + 3 = 6\n",
320-
"\n"
354+
"print(same(add(2, 2), add(3, 3)))\n"
321355
]
322356
},
323357
{
@@ -333,9 +367,17 @@
333367
},
334368
{
335369
"cell_type": "code",
336-
"execution_count": null,
370+
"execution_count": 25,
337371
"metadata": {},
338-
"outputs": [],
372+
"outputs": [
373+
{
374+
"name": "stdout",
375+
"output_type": "stream",
376+
"text": [
377+
"Hello, Bob\n"
378+
]
379+
}
380+
],
339381
"source": [
340382
"name = 'Bob'\n",
341383
"\n",
@@ -375,9 +417,21 @@
375417
},
376418
{
377419
"cell_type": "code",
378-
"execution_count": null,
420+
"execution_count": 26,
379421
"metadata": {},
380-
"outputs": [],
422+
"outputs": [
423+
{
424+
"ename": "ValueError",
425+
"evalue": "invalid literal for int() with base 10: 'This is not an integer'",
426+
"output_type": "error",
427+
"traceback": [
428+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
429+
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
430+
"Cell \u001b[0;32mIn[26], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mThis is not an integer\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
431+
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'This is not an integer'"
432+
]
433+
}
434+
],
381435
"source": [
382436
"int(\"This is not an integer\")"
383437
]
@@ -391,9 +445,30 @@
391445
},
392446
{
393447
"cell_type": "code",
394-
"execution_count": null,
448+
"execution_count": 27,
395449
"metadata": {},
396-
"outputs": [],
450+
"outputs": [
451+
{
452+
"name": "stdout",
453+
"output_type": "stream",
454+
"text": [
455+
"Converting 0 to an integer 0\n",
456+
"Converting 1 to an integer 1\n",
457+
"Converting 65 to an integer 65\n"
458+
]
459+
},
460+
{
461+
"ename": "ValueError",
462+
"evalue": "invalid literal for int() with base 10: 'Bob'",
463+
"output_type": "error",
464+
"traceback": [
465+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
466+
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
467+
"Cell \u001b[0;32mIn[27], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m e \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;241m0\u001b[39m,\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m65\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mBob\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m23\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlarry\u001b[39m\u001b[38;5;124m'\u001b[39m]:\n\u001b[0;32m----> 2\u001b[0m i \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43me\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mConverting \u001b[39m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m to an integer \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mi\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
468+
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'Bob'"
469+
]
470+
}
471+
],
397472
"source": [
398473
"for e in [0,1, 65, 'Bob', 23,'larry']:\n",
399474
" i = int(e)\n",
@@ -409,9 +484,22 @@
409484
},
410485
{
411486
"cell_type": "code",
412-
"execution_count": null,
487+
"execution_count": 28,
413488
"metadata": {},
414-
"outputs": [],
489+
"outputs": [
490+
{
491+
"name": "stdout",
492+
"output_type": "stream",
493+
"text": [
494+
"Converting 0 to an integer 0\n",
495+
"Converting 1 to an integer 1\n",
496+
"Converting 65 to an integer 65\n",
497+
"Could not convert Bob to an integer\n",
498+
"Converting 23 to an integer 23\n",
499+
"Could not convert larry to an integer\n"
500+
]
501+
}
502+
],
415503
"source": [
416504
"for e in [0,1, 65, 'Bob', 23,'larry']:\n",
417505
"\n",
@@ -436,9 +524,21 @@
436524
},
437525
{
438526
"cell_type": "code",
439-
"execution_count": null,
527+
"execution_count": 33,
440528
"metadata": {},
441-
"outputs": [],
529+
"outputs": [
530+
{
531+
"ename": "ZeroDivisionError",
532+
"evalue": "division by zero",
533+
"output_type": "error",
534+
"traceback": [
535+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
536+
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
537+
"Cell \u001b[0;32mIn[33], line 13\u001b[0m\n\u001b[1;32m 5\u001b[0m my_list \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m1\u001b[39m,\u001b[38;5;241m2\u001b[39m,\u001b[38;5;241m3\u001b[39m,\u001b[38;5;241m4\u001b[39m,\u001b[38;5;241m5\u001b[39m]\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m#print(my_list['Bob']) # TypeError: list indices must be integers or slices, not str\u001b[39;00m\n\u001b[1;32m 8\u001b[0m \n\u001b[1;32m 9\u001b[0m \u001b[38;5;66;03m#print(my_list[20]) # IndexError: list index out of range\u001b[39;00m\n\u001b[1;32m 10\u001b[0m \n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m#assert False # AssertionError ( Although you don't usually catch assertions ) \u001b[39;00m\n\u001b[0;32m---> 13\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;241;43m10\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m \u001b[38;5;66;03m# ZeroDivisionError: division by zero\u001b[39;00m\n",
538+
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
539+
]
540+
}
541+
],
442542
"source": [
443543
"# Run me! Uncomment one of the code lines to see the error it produces\n",
444544
"# Put the comment back to see another error\n",
@@ -450,9 +550,9 @@
450550
"\n",
451551
"#print(my_list[20]) # IndexError: list index out of range\n",
452552
"\n",
453-
"# assert False # AssertionError ( Although you don't usually catch assertions ) \n",
553+
"#assert False # AssertionError ( Although you don't usually catch assertions ) \n",
454554
"\n",
455-
"# x = 10 / 0 # ZeroDivisionError: division by zero\n",
555+
"x = 10 / 0 # ZeroDivisionError: division by zero\n",
456556
"\n"
457557
]
458558
},
@@ -466,15 +566,24 @@
466566
},
467567
{
468568
"cell_type": "code",
469-
"execution_count": null,
569+
"execution_count": 36,
470570
"metadata": {},
471-
"outputs": [],
571+
"outputs": [
572+
{
573+
"name": "stdout",
574+
"output_type": "stream",
575+
"text": [
576+
"Got a zero division error division by zero\n",
577+
"Got an exception division by zero\n"
578+
]
579+
}
580+
],
472581
"source": [
473582
"\n",
474583
"# You can list the different types of different `except` clauses:\n",
475584
"\n",
476585
"try:\n",
477-
" pass# do something\n",
586+
" i = 10/0\n",
478587
"except ValueError as e:\n",
479588
" print(f\"Got a value error {e}\")\n",
480589
"except TypeError as e:\n",
@@ -487,7 +596,7 @@
487596
"# But there are a lot of reasons to *not* do this\n",
488597
"\n",
489598
"try:\n",
490-
" pass# do something\n",
599+
" i = 10/0\n",
491600
"except Exception as e:\n",
492601
" # Get all types of exceptions. \n",
493602
" print(f\"Got an exception {e}\")"
@@ -517,14 +626,26 @@
517626
"\n",
518627
"if 5 in [1,2,3,4,5]:\n",
519628
" print(\"It's In!\")\n",
629+
"while True:\n",
630+
" n = input(\"Give me a string: \")\n",
631+
" if n != 'q':\n",
632+
" try:\n",
633+
" int(n)\n",
634+
" except n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n",
635+
" print(\"You got one!\")\n",
636+
" else:\n",
637+
" break\n",
638+
"\n",
639+
"\n",
640+
"\n",
520641
" \n",
521642
"\n",
522643
"\n"
523644
]
524645
},
525646
{
526647
"cell_type": "code",
527-
"execution_count": null,
648+
"execution_count": 4,
528649
"metadata": {},
529650
"outputs": [],
530651
"source": [

0 commit comments

Comments
 (0)