Uke 4

Oppgave 1

x = 34  # start
while x <= 63:  # test
    if x % 2 == 0:
        print(x)
    x += 1  # update
x = 34  # start
while x <= 63:  # test
    print(x)
    x += 2  # update

# fungerer greit som svar her, men
# hva er problemet med denne løsningen
# mer generelt?

Oppgave 2

while True:
    answer = input("Hei! Vil du snakke med meg? ")
    if answer == "Nei" or answer == "nei":
        break
    print("Så kult!")

print("Ha det bra!")

Oppgave 3

for x in range(4, 24 + 1, 4):
    print(x)

Oppgave 4

for x in range(12, 27 + 1):
    if x % 2 == 0:
        print(f"{x} er et partall!")
    else:
        print(f"{x} er et oddetall!")

Oppgave 5

number = int(input("Tall: "))  # start
result = 1
while number > 1:  # test
    # print(f'current number is {number}')
    result *= number
    # print(result)
    number -= 1  # update
print(result)

Oppgave 6

row = 0
while row < 5:
    print("  " * row + "*")
    row += 1

while row < 9:
    print("  " * (8 - row) + "*")
    row += 1

Oppgave 7

for n in range(5):
    print("  " * n + "*")

for n in range(5,9):
    print("  " * (8-n) + "*")

Oppgave 8

for i in range(10, 0, -1):
    print(i)
print("LIFTOFF!")

####################

for i in range(10):
    print(10-i)
print("LIFTOFF!")

Oppgave 9

for letter in "høyteknologisenteret":
    print(letter)

Oppgave 10

start_temp = 25.0 # degC
boil_rate = 0.625 # degC / s

current_temp = start_temp  # start
t = 0 # s                  # start
while current_temp < 100.0: # test
    print(f"{t}s = {current_temp:.1f}°C")
    current_temp += boil_rate # update
    t += 1 # s                # update
        
print(f"100.0°C i {t} sekunder")

Oppgave 11

start_height = input("Stenen droppes fra høyde: ")
start_height = float(start_height) # meter

g = 9.8 # m/s^2
height = start_height
t = 0 # s
while height > 0:
    print(f"{height:.1f} m") # eller {round(height,1)}
    t += 1
    height = start_height - 9.8 * t ** 2 / 2
print("0 m")

print(f"Stenen lander mellom {t-1} og {t} sekunder etter at den droppes.")

Oppgave 12

bin_num = input("Binært tall: ")

exponent = len(bin_num) - 1
num = 0
for ch in bin_num:
    num += int(ch) * 2 ** exponent
    exponent -= 1

print(num)