Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise <a name=\"exercise\"></a>\n",
"\n",
"## 1. Fibonacci\n",
"Create a function `fibonacci()` which takes an integer `num` as an input and returns a list of the fibonacci numbers up to `num`.\n",
"\n",
"Eg.\n",
"\n",
"Input: `8`\n",
"\n",
"Output: `[1, 1, 2, 3, 5, 8, 13, 21]`\n",
"\n",
"*Hint: You might want to recall [fibonacci numbers](https://en.wikipedia.org/wiki/Fibonacci_number)*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def fibonacci(num):\n",
" out = []\n",
" for i in range(0,num):\n",
" if(i < 2):\n",
" out.append(1)\n",
" else:\n",
" out.append(out[i-1] + out[i-2])\n",
" \n",
" return out\n",
" \n",
" \n",
"\n",
"################ Checking code ########################\n",
"# Please don't edit this code\n",
"newList = fibonacci(10)\n",
"if newList == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]:\n",
" print(\"Success!\")\n",
"else:\n",
" print(\"Error! Your function returned\")\n",
" print(newList)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Guessing game\n",
"Ask the user to input a number and then have the programm guess it. After each guess, the user must input whether it was too high, too low or the correct number. In the end, the program must always guess the users number and it must print out the number of guesses it needed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num = input()\n",
"guess = 50\n",
"tries = 0\n",
"upLimit = 100\n",
"downLimit = 0\n",
"\n",
"while (not(num == guess)):\n",
" print(\"I guess\", guess)\n",
" isCorrect = input()\n",
" if (isCorrect == \"=\"):\n",
" print(\"Guessed the number in\", tries, \"tries\")\n",
" break\n",
" if (isCorrect == \"+\"):\n",
" downLimit = guess\n",
" elif isCorrect == \"-\":\n",
" upLimit = guess\n",
" guess = int((upLimit - downLimit)/2 + downLimit)\n",
" tries = tries + 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"num = int(input())\n",
"i = 0\n",
"guess = 50\n",
"\n",
"while guess != num:\n",
" print(\"Guess the number is\", guess)\n",
" feedback = input()\n",
" if feedback == \"+\":\n",
" guess += 1\n",
" elif feedback == \"-\":\n",
" guess -= 1\n",
" else:\n",
" print(\"incorrect number\")\n",
" i += 1\n",
"\n",
"print(\"Guess your number in\", i, \"tries!\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}