"The \"straight-forward\" solution to this is a double loop with print() in the inner loop. This program will be very slow:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for x in range(24):\n",
" for y in range(60):\n",
" print(x + \":\" + y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The reason for this is that printout on the screen is time-consuming. If you want a faster program will it be smart to print fewer times and write bigger chunks of texts each time. You can also gather everything and print in the end:"
"This will be much faster than the first version.\n",
"\n",
"But if you want it to be slower, that a new time should be shown when a minute has passed can we use the first code with a little modification:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
" \n",
"for x in range(24):\n",
" for y in range(60):\n",
" print(x + \":\" + y)\n",
" time.sleep(60)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also google and find other functions in the time-module that can be used to get the program to start with the current time instead of at 0:0.\n",
"\n",
"c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for x in range(1, 11):\n",
" for y in range(1, 11):\n",
" print(x*y)\n",
" \n",
"#nicer printout:\n",
"for x in range(1, 11):\n",
" for y in range(1, 11):\n",
" print(x*y, end=' ')\n",
" print()"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
\ No newline at end of file
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
am_stud=int(input("How many students? "))
am_courses=int(input("How many courses? "))
forxinrange(1,ant_stud+1):
foryinrange(1,ant_emner+1):
print("Stud",x,"loves Course",y,end=" ; ")
print()
```
%% Cell type:markdown id: tags:
EXTRA-TASK (You can choose other courses, names, ....):
%% Cell type:code id: tags:
``` python
fromrandomimportrandom
stud=['Ada','Grace','Guido','Linus','Runa']
courses=['Ex.phil.','Ex.fac.','Matte','ITGK']
forstinstud:
print(st,end=" ")
forcoursincourses:
num=random()
ifnum>0.9:
verb='loves'
elifnum>0.6:
verb='digs'
elifnum>0.3:
verb='likes'
else:
verb='"likes"'
print(verb,cours,end="; ")
print()
```
%% Cell type:markdown id: tags:
b)
The "straight-forward" solution to this is a double loop with print() in the inner loop. This program will be very slow:
%% Cell type:code id: tags:
``` python
forxinrange(24):
foryinrange(60):
print(x+":"+y)
```
%% Cell type:markdown id: tags:
The reason for this is that printout on the screen is time-consuming. If you want a faster program will it be smart to print fewer times and write bigger chunks of texts each time. You can also gather everything and print in the end:
%% Cell type:code id: tags:
``` python
out_string=""
forxinrange(24):
foryinrange(60):
out_string+=str(x)+":"+str(y)+"\n"
# "\n" is new line
print(out_string)
```
%% Cell type:markdown id: tags:
This will be much faster than the first version.
But if you want it to be slower, that a new time should be shown when a minute has passed can we use the first code with a little modification:
%% Cell type:code id: tags:
``` python
importtime
forxinrange(24):
foryinrange(60):
print(x+":"+y)
time.sleep(60)
```
%% Cell type:markdown id: tags:
You can also google and find other functions in the time-module that can be used to get the program to start with the current time instead of at 0:0.