"## Warmup 1: Jupyter Notebook stuck in `[*]`?\n",
"\n",
"Press the Stop button! It's probably waiting for input. Otherwise, **restart** the kernel! (and run the cells)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Type in a number: 220\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"The number is 220 then 221 then 222 then 223 then 224!\n"
]
}
],
"source": [
"x = int(input(\"Type in a number: \"))\n",
"print(\"The number is \" + str(x), x + 1, x + 2, x + 3, x + 4, sep=\" then \", end=\"!\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Warmup 2: Write a function that prints the factorial of parameter num"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def do_factorial(num):\n",
" factorial = 1\n",
" while num != 1:\n",
" factorial = factorial * num\n",
" num -= 1\n",
" return factorial"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"6\n",
"24\n"
]
}
],
"source": [
"print(do_factorial(1))\n",
"print(do_factorial(2))\n",
"print(do_factorial(3))\n",
"print(do_factorial(4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Warmup 3: Complete the code to print a treasure map\n",
"The map should be a grid of size `width` and `height` with `symbol` everywhere, expect at the location: `treasure_row`, `treasure_col`, where an `'X'` is placed."
" print(str(per_early) + \"% of \" + str(target_age) + \" year olds are early birds\")\n",
" \n",
" \n",
"print_early_birds(20)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 6: What is the age of the oldest student?"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The oldest student is 40\n"
]
}
],
"source": [
"# Python reserved keyword `max` should not be used as a variable name\n",
"max_age = 0\n",
"for i in range(project.count()):\n",
" if project.get_age(i) == \"\":\n",
" continue\n",
" student_age = int(project.get_age(i))\n",
" if student_age != \"\" and student_age > max_age:\n",
" max_age = student_age \n",
"print(\"The oldest student is\", max_age)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 7: What is the age of the youngest student?"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The youngest student is 17\n"
]
}
],
"source": [
"min_age = 100\n",
"for i in range(project.count()):\n",
" if project.get_age(i) == \"\":\n",
" continue\n",
" student_age = int(project.get_age(i))\n",
" if student_age != \"\" and student_age < min_age:\n",
" min_age = student_age \n",
"print(\"The youngest student is\", min_age)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
%% Cell type:markdown id: tags:
## Warmup 1: Jupyter Notebook stuck in `[*]`?
Press the Stop button! It's probably waiting for input. Otherwise, **restart** the kernel! (and run the cells)
%% Cell type:code id: tags:
``` python
x=int(input("Type in a number: "))
print("The number is "+str(x),x+1,x+2,x+3,x+4,sep=" then ",end="!\n")
```
%% Output
Type in a number: 220
The number is 220 then 221 then 222 then 223 then 224!
%% Cell type:markdown id: tags:
## Warmup 2: Write a function that prints the factorial of parameter num
%% Cell type:code id: tags:
``` python
defdo_factorial(num):
factorial=1
whilenum!=1:
factorial=factorial*num
num-=1
returnfactorial
```
%% Cell type:code id: tags:
``` python
print(do_factorial(1))
print(do_factorial(2))
print(do_factorial(3))
print(do_factorial(4))
```
%% Output
1
2
6
24
%% Cell type:markdown id: tags:
## Warmup 3: Complete the code to print a treasure map
The map should be a grid of size `width` and `height` with `symbol` everywhere, expect at the location: `treasure_row`, `treasure_col`, where an `'X'` is placed.