Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepython
#Alternativ 1
def absolutt(tall):
    if tall < 0:
        return tall * -1
    else:
        return tall
         
#Alternativ 2
fromimport math import abs
def absolutt(tall):
    return abs(tall)

Oppgave 5 - Minste tall

Code Block
languagepython
 defdef minsteTall(a,b):
    if a < b:
        return a
    elif b < a:
        return b
    else:
        return a;
def minsteAvTreTall(a,b,c):
    if a < b &&and a < c:
        return a
    elif b < a &&and b < c:
        return b
    elif c < a &&and c < b:
        return c
    else:
        if a == b:
            return a
        elif a == c:
            return a
        elif b == c:
            return b
        #Om vi kommer ned hit betyr det at alle tallene er like
        else:
            return a
#En bedre løsning som benytter en liste
def minsteAvTreTallList(a,b,c):
    liste = [a,b,c]
    minste = a
    for tall in liste:
        if tall < minste:
            minste = tall
    return minste

...

Code Block
languagepython
def sumAvTall(n):
    sum = 0
    for i in range(1,n+1):
        sum += i
    return sum
n = 0
x = 0
while x < 500:
    n += 1
    x = sumAvTall(n)
print( n,'is the smallest number whose sum of preceeding numbers and itself is larger than 500')

Oppgave 14 - Vårt første lille program

...