...
Code Block | ||
---|---|---|
| ||
capacity = 20 currentLevel = 5 while currentLevel < capacity: print('Badekaret har', currentLevel, 'liter vann.') currentLevel += 1 print('Badekaret er fullt.') |
Oppgave 10 - Sum av gangetabellen
Code Block | ||
---|---|---|
| ||
summ = 0
for i in range(1,11):
for j in range(1,11):
summ += i * j
print(summ) |
Oppgave 11 - Stigende sekvenser
Code Block | ||
---|---|---|
| ||
import sys
j = 1
for i in range(1,11):
k = 1
while k <= j:
sys.stdout.write(str(k) + ' ')
k += 1
i += 1
j += 1
print() |
Oppgave 12 - Sjakkbrett
Code Block | ||
---|---|---|
| ||
def checkColorOfChessboard(x,y):
# we define a chessboard using nested arrays
# b means black, and w means white
board = [['w','b','w','b','w','b','w','b'],
['b','w','b','w','b','w','b','w'],
['w','b','w','b','w','b','w','b'],
['b','w','b','w','b','w','b','w'],
['w','b','w','b','w','b','w','b'],
['b','w','b','w','b','w','b','w'],
['w','b','w','b','w','b','w','b'],
['b','w','b','w','b','w','b','w']]
x2 = 8 - x
y2 = y - 1
if board[x2][y2] == 'w':
print('Rute',str(x) + ','+ str(y),'er hvit')
else:
print('Rute',str(x) + ','+ str(y),'er svart')
for x in range(1,9):
for y in range(1,9):
checkColorOfChessboard(x,y)
#alternativ løsning:
def chess_board(x,y):
if not (0<x<9 and 0<y<9):
return "Ugyldig input"
if x%2==1:
if y%2==1:
return "svart"
return "hvit"
if y%2==1:
return "hvit"
return "svart"
for i in range(1,9):
for j in range(1,9):
print("Rute",str(i)+','+str(j),"er",chess_board(i,j)) |
Oppgave 11 - Sum av gangetabellen
Code Block | ||
---|---|---|
| ||
summ = 0 for i in range(1,11): for j in range(1,11): summ += i * j print(summ) |
Oppgave 12 - Stigende sekvenser
Code Block | ||
---|---|---|
| ||
import sys j = 1 for i in range(1,11): k = 1 while k <= j: sys.stdout.write(str(k) + ' ') k += 1 i += 1 j += 1 print() |
...