Skip to content

Commit bbf4a91

Browse files
author
Charlotte Weaver
committed
upgrading lesson 3 to python 3
1 parent c4849e0 commit bbf4a91

2 files changed

Lines changed: 40 additions & 38 deletions

File tree

Lesson03_Functions/Functions.ipynb

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
"source": [
3232
"my_string = \"Howdy, welcome to my ranch?\"\n",
3333
"\n",
34-
"print len(my_string)\n",
35-
"print max(my_string)\n",
36-
"print min(my_string)"
34+
"print(len(my_string))\n",
35+
"print(max(my_string))\n",
36+
"print(min(my_string))"
3737
]
3838
},
3939
{
@@ -76,13 +76,13 @@
7676
},
7777
"outputs": [],
7878
"source": [
79-
"print int('5')\n",
80-
"print float(5)\n",
81-
"print str(5)\n",
82-
"print bool(1)\n",
79+
"print(int('5'))\n",
80+
"print(float(5))\n",
81+
"print(str(5))\n",
82+
"print(bool(1))\n",
8383
"\n",
8484
"# Uh-Oh\n",
85-
"print int('five')"
85+
"print(int('five'))"
8686
]
8787
},
8888
{
@@ -129,7 +129,7 @@
129129
"import random\n",
130130
"\n",
131131
"#Your value should be different from mine.\n",
132-
"print random.random()"
132+
"random.random()"
133133
]
134134
},
135135
{
@@ -151,7 +151,7 @@
151151
},
152152
"outputs": [],
153153
"source": [
154-
"print random.randint(5,7)"
154+
"random.randint(5,7)"
155155
]
156156
},
157157
{
@@ -213,11 +213,11 @@
213213
"# using constant\n",
214214
"radius = 5\n",
215215
"circumference = math.pi * 2 * radius\n",
216-
"print circumference\n",
216+
"print(circumference)\n",
217217
"\n",
218218
"# using functions\n",
219-
"print math.sin(0)\n",
220-
"print math.cos(0)"
219+
"print(math.sin(0))\n",
220+
"print(math.cos(0))"
221221
]
222222
},
223223
{
@@ -263,7 +263,7 @@
263263
"outputs": [],
264264
"source": [
265265
"def yeehaw():\n",
266-
" print \"YEEHAW\"\n",
266+
" print(\"YEEHAW\")\n",
267267
" \n",
268268
"#notice that nothing is printed, the functions is not run unless you call it"
269269
]
@@ -297,7 +297,7 @@
297297
"source": [
298298
"def cowgirl():\n",
299299
" yeehaw()\n",
300-
" print \"I'm gonna lasso me some cattle\"\n",
300+
" print(\"I'm gonna lasso me some cattle\")\n",
301301
" yeehaw()\n",
302302
" \n",
303303
"# Got to run it\n",
@@ -330,7 +330,7 @@
330330
"You can have your function take one or more variables as input, these are called arguments. Arguments are assigned to parameters (named variables). You can use the parameters inside the body of the function\n",
331331
"\n",
332332
" def myfunc(parameter1, parameter2):\n",
333-
" print parameter1\n",
333+
" print(parameter1)\n",
334334
" \n",
335335
" myfunc('hello', 'goodbye')\n",
336336
" \n",
@@ -348,11 +348,11 @@
348348
"source": [
349349
"def feed_animal(type_of_feed):\n",
350350
" if type_of_feed == 'oats':\n",
351-
" print \"Feeding the horse\"\n",
351+
" print(\"Feeding the horse\")\n",
352352
" elif type_of_feed == 'hay':\n",
353-
" print \"Feeding the cows\"\n",
353+
" print(\"Feeding the cows\")\n",
354354
" else:\n",
355-
" print \"Feeding the pigs\"\n",
355+
" print(\"Feeding the pigs\")\n",
356356
" \n",
357357
"feed_animal('hay')\n",
358358
"\n",
@@ -392,7 +392,7 @@
392392
" return \"Hello\"\n",
393393
" \n",
394394
" greeting = myfunction()\n",
395-
" print myfunction() + \" , world\""
395+
" print(myfunction() + \" , world\")"
396396
]
397397
},
398398
{
@@ -410,14 +410,14 @@
410410
" elif type_of_feed == 'corn':\n",
411411
" cost = 5\n",
412412
" elif type_of_feed == 'grass':\n",
413-
" return 0\n",
413+
" cost = 0\n",
414414
" return cost\n",
415415
"\n",
416416
"oats_cost = cost_of_feed('oats')\n",
417417
"\n",
418418
"# Now I can use this value\n",
419419
"oats_for_farm = 150 * oats_cost\n",
420-
"print \"It costs \" + str(oats_for_farm) + \" to feed 150 horse\""
420+
"print(\"It costs \" + str(oats_for_farm) + \" to feed 150 horse\")"
421421
]
422422
},
423423
{
@@ -480,22 +480,23 @@
480480
}
481481
],
482482
"metadata": {
483+
"anaconda-cloud": {},
483484
"kernelspec": {
484-
"display_name": "Python 2",
485+
"display_name": "Python [default]",
485486
"language": "python",
486-
"name": "python2"
487+
"name": "python3"
487488
},
488489
"language_info": {
489490
"codemirror_mode": {
490491
"name": "ipython",
491-
"version": 2
492+
"version": 3
492493
},
493494
"file_extension": ".py",
494495
"mimetype": "text/x-python",
495496
"name": "python",
496497
"nbconvert_exporter": "python",
497-
"pygments_lexer": "ipython2",
498-
"version": "2.7.11"
498+
"pygments_lexer": "ipython3",
499+
"version": "3.5.2"
499500
}
500501
},
501502
"nbformat": 4,

Lesson03_Functions/yahtzeeSOLUTION.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@ def yahtzee():
1313
die4 = roll_die()
1414
die5 = roll_die()
1515
if die1 == die2 == die3 == die4 == die5:
16-
print "Yahtzee!!"
16+
print("Yahtzee!!")
1717
return str(die1) + ", " + str(die2) + ", " +str(die3) + ", " +str(die4) + ", " +str(die5)
1818

1919

2020
# Calls yahtzee 10 times
21-
print yahtzee()
22-
print yahtzee()
23-
print yahtzee()
24-
print yahtzee()
25-
print yahtzee()
26-
print yahtzee()
27-
print yahtzee()
28-
print yahtzee()
29-
print yahtzee()
30-
print yahtzee()
21+
print(yahtzee())
22+
print(yahtzee())
23+
print(yahtzee())
24+
print(yahtzee())
25+
print(yahtzee())
26+
print(yahtzee())
27+
print(yahtzee())
28+
print(yahtzee())
29+
print(yahtzee())
30+
print(yahtzee())
31+

0 commit comments

Comments
 (0)